Why it matters

LCA is core tree query. Binary lifting is standard efficient method.

Advertisement

The architecture

Precompute up[v][k] = 2^k-th ancestor.

Query: lift deeper node up, then both together.

LCA binary liftingPrecompute up[v][k]O(n log n)Level equalizelift deeper nodeLift togetheruntil parents sameAlternative: Euler tour + RMQ for O(1) query after O(n log n)
LCA.
Advertisement

How it works end to end

Precompute: up[v][0] = parent; up[v][k] = up[up[v][k-1]][k-1].

Query: level equalize via bits of depth diff.

Then binary search.