Short answer: Binary lifting precomputes the 2^k-th ancestor of every node in a rooted tree. The table up[k][v] is the parent of up[k−1][v] taken 2^{k−1} more steps, and it fills in O(n log n). The lowest common ancestor of two nodes is found by lifting the deeper node up to the same depth and then lifting both until their parents match, in O(log n) steps. The same table evaluates any ancestor query.
What the problem actually asks
The lowest common ancestor is the deepest node that lies on both root paths. A naive walk up from both nodes is O(n) on a long path. If each node knows its ancestor 1, 2, 4, 8, … steps up, a depth difference written in binary is a sequence of those jumps, and the first meeting point can be found by the same jumps from the top down or from the bottom up.
The tree must be rooted, and every node needs a depth. A forest is fine if each tree is rooted and queries stay inside one tree. An edge change invalidates the ancestors above the edit. The basic table is for a static tree.
What the algorithm guarantees
Preprocessing is O(n log n) time and memory. Each query is O(log n). The ancestor 2^k steps above v is undefined at the root side, and the table stores a sentinel there. Jumping a node past that sentinel without a check lifts off the tree. Extra information stored beside the ancestor, such as the maximum edge on the jump, turns the same table into a path-aggregate structure.
The method is not Tarjan’s offline algorithm, which answers a batch of queries in nearly linear time with union-find during one depth-first search and cannot answer a new query without more work. Binary lifting answers online queries. Euler-tour techniques with a sparse table also answer static LCA in O(1) after O(n log n) preprocessing, by a different reduction.
The working steps
- Root the tree, compute depth[v] and up[0][v] = parent[v] by a depth-first or breadth-first search. The root’s parent is a sentinel.
- For k from 1 to floor(log₂ n), set up[k][v] = up[k−1][ up[k−1][v] ], with the sentinel unchanged.
- To find the LCA of a and b, assume depth[a] ≥ depth[b]. Lift a by the binary digits of depth[a] − depth[b] so the depths match.
- If a and b are now the same node, that node is the answer.
- Otherwise lift both, from the largest k downward, whenever up[k][a] and up[k][b] differ. The parents of the final a and b are the LCA.
The O(log n) query needs the ancestor table of a static rooted tree. It does not handle edge insertions. A link-cut tree or an Euler-tour tree is the dynamic alternative and a different structure. The final step returns the parent of the two distinct nodes, not the nodes themselves, when the loop lifts while the ancestors differ.
Approaches that look simpler and fail
| Approach | Why it fails when the last lift returns the node instead of its parent |
|---|---|
| Return a after the differing-ancestor loop | The loop stops with a and b sitting just below the LCA. The answer is their shared parent. |
| Lift the shallower node by the full depth difference | That walk passes the root or misses the meeting level. Only the deeper node moves in the first phase. |
| Fill up[k][v] from the parent pointer by walking 2^k steps | That preprocessing is O(n · n). The doubling recurrence fills the same table in O(n log n). |
| Use the table after the tree is rerooted | Every ancestor pointer was computed for the old root. The table has to be rebuilt. |
A batch of LCA queries on a static tree can use Tarjan’s offline union-find pass. A constant-time query can use an Euler tour plus a sparse table. Binary lifting is the online O(log n) method that also generalizes to path aggregates.
A working example
This Python fragment lifts the deeper node first, then jumps on bits where the 2^k ancestors differ. The answer is the parent of the node where those ancestors diverge. It prints the LCA of 4 and 5, which is 1.
# parent: 1 and 2 under 0, 3 and 4 under 1, 5 under 2
parent = [-1, 0, 0, 1, 1, 2]
depth = [0, 1, 1, 2, 2, 2]
n = len(parent)
LOG = n.bit_length()
up = [[0] * n for _ in range(LOG)]
for v in range(n):
up[0][v] = parent[v] if parent[v] >= 0 else v
for k in range(1, LOG):
for v in range(n):
up[k][v] = up[k - 1][up[k - 1][v]]
def lca(a, b):
if depth[a] < depth[b]:
a, b = b, a
diff = depth[a] - depth[b]
bit = 0
while diff:
if diff & 1:
a = up[bit][a]
diff >>= 1
bit += 1
if a == b:
return a
for k in range(LOG - 1, -1, -1):
if up[k][a] != up[k][b]:
a, b = up[k][a], up[k][b]
return parent[a]
assert lca(4, 3) == 1 and lca(4, 5) == 0 and lca(3, 1) == 1
print(lca(4, 3))
Where it is used
Binary lifting answers a lowest-common-ancestor query after a tree is built: the deepest common folder, the deepest common router, or the meeting point of two nodes in a hierarchy. A batch of queries known in advance can use Tarjan’s offline method instead.
When you can drop this algorithm
Drop binary lifting when the tree changes between queries. The table will lie. Keep it for a static rooted tree with online ancestor and LCA queries, and remember that the last step returns the parent.
Why this stays published
We publish this so a search for binary lifting returns the doubling table and the final parent step of the LCA query. Binary lifting answers ancestor and lowest-common-ancestor queries in O(log n) time after an O(n log n) table of 2^k-th parents.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
