Lowest Common Ancestor
Find the deepest shared ancestor of two nodes in a rooted tree. LCA is a core tree-query problem that ties together DFS, binary lifting, and Euler tours.
Trees is nested under Graphs , so techniques in the broader family usually still apply here.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
node 0
parent: none
depth: 0
node 1
parent: 0
depth: 1
node 2
parent: 0
depth: 1
node 3
parent: 1
depth: 2
node 4
parent: 1
depth: 2
node 5
parent: 2
depth: 2
node 6
parent: 5
depth: 3
Path from 3 to root
Path from 6 to root
First shared ancestor
The lowest common ancestor of 3 and 6 is 0: the deepest node that lies on both root paths.
Family
Graphs → Trees
Hierarchies and constrained graphs. A tree is a connected acyclic graph.
Builds on
2 topics
Read these first if you want the surrounding context.
Unlocks
1 next topic
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
2
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
Given two nodes in a rooted tree, find the deepest node that is an ancestor of both.
That node is their lowest common ancestor (LCA).
This query appears everywhere:
- distance between two tree nodes
- path queries
- subtree reasoning
- comparing ancestry relationships
Why it matters
LCA is one of the big “tree queries” that turns a plain rooted tree into a query-processing structure.
It also acts as a meeting point for several other topics:
- DFS provides depth and parent information
- Binary lifting gives one common solution
- Euler tour + sparse table gives another classic route
So LCA is not isolated. It sits exactly where the tree-query toolkit comes together.
Core idea
A node is a common ancestor if both query nodes lie in its subtree chain to the root.
The lowest common ancestor is simply the deepest such node.
A slow solution walks both nodes upward until they meet. A fast solution preprocesses the tree.
Binary lifting route
- preprocess depth and parent jumps
- lift the deeper node until both nodes have the same depth
- lift both nodes together from large jumps to small jumps
- the parent where they first agree is the LCA
This gives:
- preprocessing:
- query:
Euler tour + RMQ route
Another classic method:
- run an Euler tour of the tree
- record depths along the tour
- the LCA of two nodes becomes a range minimum query over first occurrences
This is why LCA often appears right after Euler tour and sparse table material.
Complexity
| Method | Preprocess | Query |
|---|---|---|
| Naive parent climbing | ||
| Binary lifting | ||
| Euler tour + RMQ | or depending on RMQ structure |
Key takeaways
- LCA is a core rooted-tree query, not a niche trick
- It ties together depth, parent pointers, DFS order, and range-query ideas
- Binary lifting is usually the most practical general solution
- Euler tour + RMQ is the more reduction-style view of the same problem
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Lowest Common Ancestor of a Binary Tree | 🟡 Medium | Basic recursive LCA reasoning |
| Company Queries II | 🔴 Hard | Fast LCA queries in rooted trees |
| Distance Queries | 🔴 Hard | LCA plus depth arithmetic |
Relation to other topics
- Binary lifting is the most common fast LCA implementation in this atlas
- Euler tour technique can reduce LCA to a range minimum query
- Sparse table is often the RMQ structure used after Euler tour preprocessing
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
What this enables
Once the current idea feels natural, these are the most useful next jumps.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Euler Tour Technique
Flatten a rooted tree into an array so subtree problems become range problems. This is the bridge between trees and range-query structures.
Heavy-Light Decomposition
Break tree paths into O(log n) heavy-chain segments so path queries can ride on top of a segment tree instead of walking edge by edge.
Sparse Table
Answer static range minima, maxima, or gcd queries in O(1) after O(n log n) preprocessing. Sparse tables are pure precomputation power.
More from Trees
Stay in the same family when you want parallel variations of the same mental model.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
Binary Search Tree (BST)
A sorted tree structure enabling O(log n) search, insert, and delete. Foundation for balanced trees, sets, and maps.
Heap & Priority Queue
A complete binary tree where every parent is smaller (or larger) than its children. The backbone of Dijkstra, event scheduling, and top-k problems.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Rooted tree queries
Flatten subtrees, jump through ancestors, and break tree paths into array segments instead of climbing naively.
Tree path queries
Build the full ladder from ancestor queries to heavy-light decomposition and historical segment trees.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.