Smallest Deepest Subtree

Given a binary tree rooted atroot, the _depth _of each node is the shortest distance to the root.

A node is _deepest _if it has the largest depth possible among any node in theentire tree.

The subtree of a node is that node, plus the set of all descendants of that node.

Return the node with the largest depth such that it contains all the deepest nodes in its subtree.

Example

Note

如果左右子树一样深,那么这个root就是答案

如果左子树更深,那么答案在左子树,进行递归

同理,如果右子树更深,那么答案在右子树,继续递归

Code

Last updated