531. Deepest Leaves' Ancestor
A company stores its reporting structure as a binary tree root: each employee has at most two direct reports, a left one and a right one. The employees found at the greatest depth, those furthest from the top boss, are the most junior staff. Return the lowest employee (the one furthest from the root) whose team, meaning that employee together with everyone beneath them, contains all of the most junior staff. When just one employee sits at the greatest depth, that employee is the answer. The result must be a reference to a node of the given tree. If the tree is empty, return null.
Example 1
- Input:
- root = [12,5,18,3,8,null,21,null,null,null,9]
- Output:
- 6
- Explanation:
Value 9 is the only node at depth 3, so it is its own answer.
Example 2
- Input:
- root = [6,2,9,1,4,7,11]
- Output:
- 0
- Explanation:
The four leaves 1, 4, 7 and 11 are all deepest, and only the root 6 has all of them below it.
Example 3
- Input:
- root = [30,14,40,9,20,null,null,null,12,17]
- Output:
- 1
- Explanation:
The deepest nodes are 12 and 17 (depth 3), and 14 is the lowest node that has both in its subtree.
Constraints
0 ≤ number of nodes ≤ 500
-105 ≤ Node.val ≤ 105
All node values are distinct.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(h)