2Brute2Best
🔥0

Lowest Common Ancestor of Deepest Leaves

Medium·LeetCode #1123 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given the root of a binary tree. Find the deepest leaves: the leaves that are farthest from the root. The root has depth 0, and each child is one level deeper than its parent.

Return the lowest common ancestor of all of those deepest leaves. That is the lowest node that has every deepest leaf below it. A node counts as its own ancestor, so if there is only one deepest leaf, the answer is that leaf itself.

Input

root, the root of a binary tree (it has at least one node).

Output

The node that is the lowest common ancestor of all the deepest leaves.

Example 1
Input: root = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]
Output: 2

The deepest leaves are 7 and 4, at depth 3. The leaves 6, 0 and 8 are also leaves, but they sit at depth 2. Both 7 and 4 are below 2, so the answer is 2.

Example 2
Input: root = [1]
Output: 1

The only node is a leaf, so it is its own answer.

Example 3
Input: root = [0, 1, 3, null, 2]
Output: 2

The only deepest leaf is 2, at depth 2, so the answer is 2 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.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“deepest leaves”
→ Depth is measured from the root, and only the deepest leaves count. Each subtree has to report how deep it goes.
“lowest common ancestor of all of them”
→ This is a set of leaves, not just two. The LCA of the set is the node where the deepest leaves split.
“returns one node”
→ The answer is a single node, so a pair (height, LCA) from each child is enough to build the answer.
03

Approach Complexity Ladder

Brute Force — BFS for the deepest leaves, then fold the LCAO(N·K)O(N)

Level-by-level search collects the deepest leaves; combine them two at a time with the LCA from LC 236

Optimal — one post-order pass with (height, LCA) pairsO(N)O(H)

Each node returns its subtree's height and the LCA of that subtree's deepest leaves

Brute Force Intuition

A BFS goes level by level, so its last level is the deepest one, and every node on it is a leaf. Once we have those leaves, the LCA of a set can be built by folding: take the LCA of the first two leaves, then the LCA of that result with the third, and so on. Each step is the LC 236 LCA.

Recognition rule for this sub-pattern:

The LCA is the first node where the two targets split into different subtrees.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.