Skip to main content
Back to the DSA atlas
GraphsTrees Technique Advanced

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.

treedoublinglcarare

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

Jump table

node2^02^12^2
0---
10--
20--
310-
410-
520-
652-

Bit decomposition of k = 3

use 2^0: 6 -> 5
use 2^1: 5 -> 0

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 kk-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 O(n)O(n) in the worst case. Binary lifting reduces that to O(logn)O(\log n).

Core idea

Precompute up[u][j] = the 2j2^j-th ancestor of node u.

Examples:

  • up[u][0] = parent of u
  • up[u][1] = 2nd ancestor
  • up[u][2] = 4th ancestor
  • up[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:

k=13=8+4+1k = 13 = 8 + 4 + 1

then jump by:

  • 232^3
  • 222^2
  • 202^0

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

OperationTimeSpace
PreprocessingO(nlogn)O(n \log n)O(nlogn)O(n \log n)
One k-th ancestor queryO(logn)O(\log n)-

LCA connection

Lowest common ancestor is often built on top of binary lifting:

  1. raise the deeper node until both are at the same depth
  2. try large jumps downward from the top bit to the bottom bit
  3. 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

ProblemDifficultyKey idea
Kth Ancestor of a Tree Node🟡 MediumDirect binary lifting
Company Queries I🟡 MediumRooted tree ancestor jumps
Lowest Common Ancestor🔴 HardBinary 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.

More from Trees

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

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.