Graph Theory37 sections · 1633 units
Open in Course

Binary Lifting - Climbing Together

(Find the meeting point)

After both nodes are at the same depth, you climb them upward together. Start from the highest power of 22 and work down. For each power kk, check if up[u][k] != up[v][k]. If they differ, jump both nodes: u = up[u][k] and v = up[v][k]. If they are the same, skip this power and try a smaller one.

When no more jumps are possible without making u and v equal, their direct parent up[u][0] is the LCA. You stop one step before they merge because jumping too far would overshoot. This runs in O(logn)O(\log n) time per query and uses O(nlogn)O(n \log n) space for the precomputed table.