Lowest Common Ancestor
Build binary-lifting ancestors, align node depths, and jump together to find their deepest shared ancestor.
Core idea
Binary lifting stores each node's ancestor one, two, four, and more powers of two above it. Queries first equalize depths, then lift both nodes without jumping above their meeting point.
Read the visualization
The table builds from smaller jumps to larger ones. During a query, highlighted powers explain each depth alignment and synchronized jump before the shared parent is returned.
| depth | up⁰ | up¹ | up² | |
|---|---|---|---|---|
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 |
Root the tree and prepare depths plus powers-of-two ancestor columns.
Complexity and tradeoffs
Time: Build O(V log V); query O(log V). Space: O(V log V). Binary lifting answers many static-tree ancestor and distance queries efficiently.
Where it fits
LCA preprocessing supports repeated ancestor, tree-distance, path, and virtual-tree queries on a static rooted tree. Dynamic trees require different machinery.