binary tree
6
5
7
2
4
3
0
1
8
call stack ↓
(returned)
Recursive DFS (bubble up)
▸1lca(node):2 if node is null: return null3 if node is p or node is q: return node // found a target4 left = lca(node.left)5 right = lca(node.right)6 if left and right: return node // split → LCA7 return left or right // carry target up8// answer = lca(root)
state
- p5
- q1
- goaldeepest common ancestor