{"id":143,"date":"2026-11-13T12:32:36","date_gmt":"2026-11-13T04:32:36","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-binary-lifting-computes-a-lowest-common-ancestor\/"},"modified":"2026-11-13T21:51:13","modified_gmt":"2026-11-13T13:51:13","slug":"how-binary-lifting-computes-a-lowest-common-ancestor","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-binary-lifting-computes-a-lowest-common-ancestor\/","title":{"rendered":"How binary lifting computes a lowest common ancestor"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Binary lifting<\/strong> precomputes the 2^k-th ancestor of every node in a rooted tree. The table up[k][v] is the parent of up[k\u22121][v] taken 2^{k\u22121} more steps, and it fills in <strong>O(n log n)<\/strong>. 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 <strong>O(log n)<\/strong> steps. The same table evaluates any ancestor query.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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, \u2026 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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The method is not Tarjan&#8217;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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Root the tree, compute depth[v] and up[0][v] = parent[v] by a depth-first or breadth-first search. The root&#8217;s parent is a sentinel.<\/li>\n<li>For k from 1 to floor(log\u2082 n), set up[k][v] = up[k\u22121][ up[k\u22121][v] ], with the sentinel unchanged.<\/li>\n<li>To find the LCA of a and b, assume depth[a] \u2265 depth[b]. Lift a by the binary digits of depth[a] \u2212 depth[b] so the depths match.<\/li>\n<li>If a and b are now the same node, that node is the answer.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the last lift returns the node instead of its parent<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Return a after the differing-ancestor loop<\/td>\n<td>The loop stops with a and b sitting just below the LCA. The answer is their shared parent.<\/td>\n<\/tr>\n<tr>\n<td>Lift the shallower node by the full depth difference<\/td>\n<td>That walk passes the root or misses the meeting level. Only the deeper node moves in the first phase.<\/td>\n<\/tr>\n<tr>\n<td>Fill up[k][v] from the parent pointer by walking 2^k steps<\/td>\n<td>That preprocessing is O(n \u00b7 n). The doubling recurrence fills the same table in O(n log n).<\/td>\n<\/tr>\n<tr>\n<td>Use the table after the tree is rerooted<\/td>\n<td>Every ancestor pointer was computed for the old root. The table has to be rebuilt.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A batch of LCA queries on a static tree can use Tarjan&#8217;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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>1<\/code>.<\/p>\n\n<pre><code class=\"language-python\"># parent: 1 and 2 under 0, 3 and 4 under 1, 5 under 2\nparent = [-1, 0, 0, 1, 1, 2]\ndepth = [0, 1, 1, 2, 2, 2]\nn = len(parent)\nLOG = n.bit_length()\nup = [[0] * n for _ in range(LOG)]\nfor v in range(n):\n    up[0][v] = parent[v] if parent[v] &gt;= 0 else v\nfor k in range(1, LOG):\n    for v in range(n):\n        up[k][v] = up[k - 1][up[k - 1][v]]\n\ndef lca(a, b):\n    if depth[a] &lt; depth[b]:\n        a, b = b, a\n    diff = depth[a] - depth[b]\n    bit = 0\n    while diff:\n        if diff &amp; 1:\n            a = up[bit][a]\n        diff &gt;&gt;= 1\n        bit += 1\n    if a == b:\n        return a\n    for k in range(LOG - 1, -1, -1):\n        if up[k][a] != up[k][b]:\n            a, b = up[k][a], up[k][b]\n    return parent[a]\n\nassert lca(4, 3) == 1 and lca(4, 5) == 0 and lca(3, 1) == 1\nprint(lca(4, 3))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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&#8217;s offline method instead.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for binary lifting returns the doubling table and the final parent step of the LCA query. <strong>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.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u22121][v] taken 2^{k\u22121} 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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-trees"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions\/542"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}