Lowest Common Ancestor of Binary Tree
A medium Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 3
- Core for
- 4 roles
The problem
Given the root of a binary tree and two nodes p and q, return the lowest common ancestor of the two nodes. The lowest common ancestor is the deepest node that has both p and q as descendants.
Example 1
- Input
- root=[3,5,1,6,2,0,8,null,null,7,4], p=5, q=1
- Output
- 3
- Why
- Node 3 is the lowest node that has both 5 and 1 as descendants.
Example 2
- Input
- root=[3,5,1,6,2,0,8,null,null,7,4], p=5, q=4
- Output
- 5
- Why
- Node 5 is an ancestor of node 4, so 5 is their lowest common ancestor.
Example 3
- Input
- root=[1,2], p=1, q=2
- Output
- 1
- Why
- The root node 1 is the ancestor of node 2.
Constraints
- The number of nodes is in the range [2, 105].
- -109 <= Node.val <= 109
- All Node.val are unique.
- p and q are nodes in the tree.
How to think about it
Updated 2026-09-09When searching bottom-up, a node is the lowest common ancestor if both of its subtrees report finding one of the target nodes, or if the node itself is one target and its subtree contains the other. Bubble found targets upward until their search paths converge.
Approaches, worst first
Root-to-node path intersection
time O(n) · space O(h)
Find path from root to `p` and path from root to `q` using backtracking DFS. Compare both path arrays from the beginning and return the last node they share before diverging. Allocates lists for both paths.
Postorder bottom-up searchWrite this one
time O(n) · space O(h)
If current node is null, `p`, or `q`, return current node. Recurse on left and right. If both calls return non-null, current node is the LCA. If only one returns non-null, propagate that non-null node upward.
Where people lose marks · 3
- Assuming `p` and `q` must be in separate subtrees; one target can be the direct ancestor of the other, making that target its own LCA.
- Comparing node values instead of node object references when node values are not guaranteed unique across generic binary tree variations.
- Searching BST-style with value comparisons; this is a generic binary tree where keys are not ordered.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .