Lowest Common Ancestor of Deepest Leaves - Solution & Explanation
Problem Statement
Given the root of a binary tree, return the lowest common ancestor of its deepest leaves.
Recall that:
- The node of a binary tree is a leaf if and only if it has no children
- The depth of the root of the tree is
0. if the depth of a node isd, the depth of each of its children isd + 1. - The lowest common ancestor of a set
Sof nodes, is the nodeAwith the largest depth such that every node inSis in the subtree with rootA.
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:
- The number of nodes in the tree will be in the range
[1, 1000]. 0 <= Node.val <= 1000- The values of the nodes in the tree are unique.
Note: This question is the same as 865: https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
Approach Overview
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.
Approach 1: Recursive Deepest Leaves and LCA
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.
Complexity
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.
Approach 2: Iterative Deepest Leaves and LCA using BFS
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.
Complexity
Time Complexity: O(N), as each node is enqueued and dequeued once.
Space Complexity: O(N), associated with the storage requirements of the queue.
Approach 3: DFS
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:
- If
rootis null, return the tuple(None, 0); - Otherwise, we recursively call
dfs(root.left)anddfs(root.right), obtaining tuples(l, d1)and(r, d2). Ifd1 > d2, the deepest common ancestor ofrootisl, and the depth isd1 + 1; ifd1 < d2, the deepest common ancestor ofrootisr, and the depth isd2 + 1; ifd1 = d2, the deepest common ancestor ofrootisroot, and the depth isd1 + 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.
Complexity Comparison
| 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 | — |
Detailed Complexity Analysis
| 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. |
Video Solution
Lowest Common Ancestor of Deepest Leaves | Smallest Subtree with all the Deepest Nodes | 1123 | 865 • codestorywithMIK • 19,362 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Lowest Common Ancestor of Deepest Leaves easy or hard?
Lowest Common Ancestor of Deepest Leaves Python/Java solution
How to solve Lowest Common Ancestor of Deepest Leaves in O(n)?
What is the best approach for Lowest Common Ancestor of Deepest Leaves?
Is Lowest Common Ancestor of Deepest Leaves asked at Google/Amazon/Meta?
What data structure is used in Lowest Common Ancestor of Deepest Leaves?
What is the time complexity of Lowest Common Ancestor of Deepest Leaves?
Ready to solve this problem?
Practice Lowest Common Ancestor of Deepest Leaves with our built-in code editor and test cases.
Practice on FleetCode