Find the lowest common ancestor of all deepest leaves in a binary tree.
Given the root of a binary tree, return the node that is the lowest common ancestor (LCA) of all the tree's deepest leaves.
A leaf is a node with no children. The deepest leaves are the leaves that have the maximum depth from the root. If there is only one deepest leaf, the answer is that leaf itself.
The LCA of a set of nodes is the deepest node in the tree that is an ancestor of every node in the set.
Write a function that identifies the subtree containing the deepest leaves and returns the node that is the common ancestor of all of them.
root of a binary tree.Example 1
Input
root = [3,5,1,6,2,0,8,null,null,7,4]
Output
[2,7,4]
Explanation
The deepest leaves are 7 and 4, and their lowest common ancestor is node 2.
Example 2
Input
root = [1]
Output
[1]
Explanation
The single node is both the only leaf and the LCA of the deepest leaves.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.