Skip to main content

Lowest Common Ancestor of Deepest Leaves - Solution & Explanation

MediumHash TableTreeDepth-First SearchBreadth-First Search23 min readAsked at: Amazon, Meta, Google +1
Practice this problem

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 is d, the depth of each of its children is d + 1.
  • The lowest common ancestor of a set 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:

  • 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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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 root is null, return the tuple (None, 0);
  • Otherwise, we recursively call 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.

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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

ApproachTimeSpaceWhen 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 BFSO(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 is rated Medium difficulty. The challenge is recognizing that the deepest leaves determine the ancestor based on subtree depth comparison. Once you frame the problem as a DFS returning depth and node together, the implementation becomes straightforward.
Lowest Common Ancestor of Deepest Leaves Python/Java solution
Python and Java implementations usually follow the same DFS pattern: recursively compute left and right subtree depths, then return the node that represents the LCA when both depths match. The logic fits naturally with recursive functions in Python and helper methods in Java classes.
How to solve Lowest Common Ancestor of Deepest Leaves in O(n)?
Perform a post-order DFS where each recursive call returns the maximum depth and the LCA candidate for that subtree. Compare the left and right subtree depths. If they are equal, return the current node as the LCA; otherwise return the deeper subtree's candidate. Since every node is processed once, the complexity is O(n).
What is the best approach for Lowest Common Ancestor of Deepest Leaves?
The most efficient approach uses a single Depth-First Search traversal that returns both the deepest depth and the corresponding LCA node from each subtree. If the left and right subtree depths are equal, the current node becomes the LCA. This method runs in O(n) time and O(h) space where h is the height of the tree.
Is Lowest Common Ancestor of Deepest Leaves asked at Google/Amazon/Meta?
Tree and LCA variations frequently appear in interviews at companies like Google, Amazon, and Meta. This problem tests understanding of binary tree traversal, depth tracking, and combining subtree results, which are common interview themes.
What data structure is used in Lowest Common Ancestor of Deepest Leaves?
The primary data structure is a binary tree. The solution typically uses Depth-First Search recursion or Breadth-First Search with a queue. Some implementations also maintain parent pointers or hash maps when computing the LCA of multiple nodes.
What is the time complexity of Lowest Common Ancestor of Deepest Leaves?
The optimal solution runs in O(n) time because each node in the binary tree is visited exactly once during traversal. Recursive DFS uses O(h) space for the call stack, while a BFS-based solution may require O(n) space for the queue and parent tracking.

Ready to solve this problem?

Practice Lowest Common Ancestor of Deepest Leaves with our built-in code editor and test cases.

Practice on FleetCode