Graph Theory37 sections · 1633 units
Open in Course

Walkthrough - Different Subtrees

(Nodes in separate branches)

Tree: 11 has children 2,32, 3. Node 22 has child 44. Node 33 has child 55. Query: LCA(4,5)(4, 5). Both have depth=2depth = 2. Check up[4][1] = 1, up[5][1] = 1: same, skip.

Check up[4][0] = 2, up[5][0] = 3: different, jump both. Now a=2a = 2, b=3b = 3. No more bits to check. Return up[2][0] = 1. LCA is 11. The nodes are in different subtrees, so their meeting point is the root. This example shows how the algorithm handles nodes in separate branches.