Given the root of a binary tree, return the lowest common ancestor of its deepest leaves.
Recall that:
0. if the depth of a node is d, the depth of each of its children is d + 1.S of nodes, is the node A with the largest depth such that every node in S is in the subtree with root A.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4] Output: [2,7,4] Explanation: We return the node with value 2, colored in yellow in the diagram. The nodes coloured in blue are the deepest leaf-nodes of the tree. Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.
Example 2:
Input: root = [1] Output: [1] Explanation: The root is the deepest node in the tree, and it's the lca of itself.
Example 3:
Input: root = [0,1,3,null,2] Output: [2] Explanation: The deepest leaf node in the tree is 2, the lca of one node is itself.
Constraints:
[1, 1000].0 <= Node.val <= 1000
Note: This question is the same as 865: https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
Problem Overview: Given a binary tree, return the lowest common ancestor (LCA) of all the deepest leaf nodes. The deepest leaves are the nodes located at the maximum depth in the tree. If multiple deepest leaves exist in different subtrees, the answer is the lowest node that is an ancestor of all of them.
Approach 1: Recursive Deepest Leaves and LCA (DFS) (Time: O(n), Space: O(h))
This approach performs a single post-order traversal using Depth-First Search. For every node, compute the depth of its left and right subtrees. If both sides reach the same maximum depth, the current node becomes the lowest common ancestor of the deepest leaves in those subtrees. If one subtree is deeper, propagate the deeper subtree's candidate node upward. Each recursive call returns a pair: the deepest depth found and the corresponding LCA node. Because every node is visited exactly once, the time complexity is O(n). The recursion stack uses O(h) space where h is the tree height. This solution is concise and naturally fits problems involving Tree recursion.
Approach 2: Iterative Deepest Leaves and LCA using BFS (Time: O(n), Space: O(n))
This method first finds all deepest nodes using level-order traversal with Breadth-First Search. Iterate through the tree level by level and track the nodes in the last processed level, which represent the deepest leaves. After collecting these nodes, compute their lowest common ancestor by walking upward using parent references or by repeatedly merging pairs of nodes using an LCA routine on the Binary Tree. BFS guarantees that the final processed level contains exactly the deepest nodes. The traversal visits each node once for O(n) time and stores nodes in a queue with O(n) auxiliary space.
Recommended for interviews: The recursive DFS approach is usually preferred. It solves the problem in a single traversal and directly combines depth computation with LCA discovery. Interviewers expect you to recognize that the deepest leaves determine the LCA based on subtree depths. The BFS approach demonstrates a clear understanding of level traversal and is useful when you explicitly need to identify the deepest level first, but it typically requires extra bookkeeping.
This approach involves recursively finding the depth of the subtree at each node and returning the LCA of the deepest leaves.
The main idea is to perform a post-order traversal to determine the depth of left and right subtrees. If the depths are equal, the current node is the LCA. If the left subtree is deeper, recursively find the LCA in the left subtree, and similarly for the right subtree.
The code uses a depth-first search (DFS) to determine the depth of each subtree while simultaneously working out the LCA. The `dfs` function returns the maximum depth of the subtree and updates the `lca` when it finds a subtree with nodes as deep as the deepest nodes found so far, making this node the current LCA.
Time Complexity: O(N), where N is the number of nodes in the tree, as each node is visited once.
Space Complexity: O(H), where H is the height of the tree, representing the call stack in the worst-case scenario.
Instead of recursive DFS, another approach is to use Breadth-First Search (BFS) to traverse levels of the tree iteratively, keeping track of the nodes at the deepest level. This level-based traversal ensures we gather all deepest leaves in a set. Finally, we use a standard method to determine LCA from this set of nodes.
This C code uses basic queue operations to perform level-order traversal. The deepest leaves are at the end of traversal, thus, by tracking the last fully processed level in the queue, the common ancestor can be concluded.
Time Complexity: O(N), as each node is enqueued and dequeued once.
Space Complexity: O(N), associated with the storage requirements of the queue.
We design a function dfs(root) that returns a tuple (l, d), where l is the deepest common ancestor of node root, and d is the depth of node root. The execution logic of the function dfs(root) is as follows:
root is null, return the tuple (None, 0);dfs(root.left) and dfs(root.right), obtaining tuples (l, d1) and (r, d2). If d1 > d2, the deepest common ancestor of root is l, and the depth is d1 + 1; if d1 < d2, the deepest common ancestor of root is r, and the depth is d2 + 1; if d1 = d2, the deepest common ancestor of root is root, and the depth is d1 + 1.In the main function, we call dfs(root) and return the first element of its return value to get the deepest common ancestor node.
The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree.
| Approach | Complexity |
|---|---|
| Recursive Deepest Leaves and LCA | Time Complexity: O(N), where N is the number of nodes in the tree, as each node is visited once. Space Complexity: O(H), where H is the height of the tree, representing the call stack in the worst-case scenario. |
| Iterative Deepest Leaves and LCA using BFS | Time Complexity: O(N), as each node is enqueued and dequeued once. Space Complexity: O(N), associated with the storage requirements of the queue. |
| DFS | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Deepest Leaves + LCA (DFS) | O(n) | O(h) | Best general solution. Single traversal and minimal extra memory. |
| Iterative Deepest Leaves + LCA using BFS | O(n) | O(n) | Useful when explicitly identifying the deepest level first or when avoiding recursion. |
Lowest Common Ancestor of Deepest Leaves | Smallest Subtree with all the Deepest Nodes | 1123 | 865 • codestorywithMIK • 18,297 views views
Watch 9 more video solutions →Practice Lowest Common Ancestor of Deepest Leaves with our built-in code editor and test cases.
Practice on FleetCode