Smallest Subtree With All Deepest Nodes
You are given the root of a binary tree. The deepest nodes are those on the lowest level of the tree. Find the node that is the deepest node that has every deepest node inside its subtree, and return it, which means the whole subtree below it. If there is a single deepest node, the answer is that node. The tree has at least one node.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
root [3,5,1,6,2,0,8,null,null,7,4]
returns [2,7,4]
The deepest nodes are 7 and 4, both under 2, so the answer is the subtree [2,7,4].
Constraints
1 <= number of nodes <= 1000 <= node.val <= 500
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.