Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
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.
Jump table
| node | 2^0 | 2^1 | 2^2 |
|---|---|---|---|
| 0 | - | - | - |
| 1 | 0 | - | - |
| 2 | 0 | - | - |
| 3 | 1 | 0 | - |
| 4 | 1 | 0 | - |
| 5 | 2 | 0 | - |
| 6 | 5 | 2 | - |
Bit decomposition of k = 3
Ancestor jumping by powers of two
To find the 3-ancestor of node 6, break 3 into powers of two and follow only those jumps. Answer: 0.
Family
Graphs → Trees
Hierarchies and constrained graphs. A tree is a connected acyclic graph.
Builds on
1 topic
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
You have a rooted tree and want to answer questions like:
- what is the -th ancestor of node
u? - can we jump upward quickly?
- how do LCA-style preprocessors avoid climbing one parent at a time?
If you move one edge per step, each query can take in the worst case. Binary lifting reduces that to .
Core idea
Precompute up[u][j] = the -th ancestor of node u.
Examples:
up[u][0]= parent ofuup[u][1]= 2nd ancestorup[u][2]= 4th ancestorup[u][3]= 8th ancestor
This is the same doubling idea used by sparse tables, but the domain is a tree instead of an array.
Build
First compute parents with DFS or BFS. Then fill the jump table:
up[u][0] <- parent[u]
for j in 1..LOG:
up[u][j] <- up[ up[u][j-1] ][j-1]
If a jump leaves the tree, store null.
Query
To move up by k, inspect the binary representation of k.
If:
then jump by:
Only the set bits matter.
Why it is useful
Binary lifting directly solves:
- k-th ancestor queries
- level alignment in LCA
- many path and ancestor predicates
It is one of the standard rooted-tree preprocessors because it transforms repeated upward movement into a few table lookups.
Complexity
| Operation | Time | Space |
|---|---|---|
| Preprocessing | ||
| One k-th ancestor query | - |
LCA connection
Lowest common ancestor is often built on top of binary lifting:
- raise the deeper node until both are at the same depth
- try large jumps downward from the top bit to the bottom bit
- stop right before the nodes would become equal
That is why binary lifting is often taught as “ancestor jumps,” even though its most common use is enabling LCA.
Key takeaways
- Binary lifting is doubling on trees
- Queries work by decomposing a jump count into powers of two
- Sparse tables and binary lifting are close cousins conceptually
- It is uncommon in beginner interviews, but very important for serious tree-query work
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Kth Ancestor of a Tree Node | 🟡 Medium | Direct binary lifting |
| Company Queries I | 🟡 Medium | Rooted tree ancestor jumps |
| Lowest Common Ancestor | 🔴 Hard | Binary lifting plus depth alignment |
Relation to other topics
- Tree fundamentals gives the rooted-parent model binary lifting depends on
- Euler tour technique helps with subtree queries, while binary lifting helps with ancestor queries
- Sparse table uses the same powers-of-two precomputation pattern in arrays
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
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.
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.
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.
Treap
Combine binary-search-tree ordering with heap priorities to get a balanced randomized tree with elegant split and merge operations.
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.
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.
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.