Back to problems Solve on LeetCode → See #700 Search BST →

Lowest Common Ancestor of BST

LeetCode 235 • Easy • Trees

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8  →  Output: 6
LCA: first node where p and q diverge. If both < root go left, both > root go right, else root is LCA.

TimeO(h)path height
SpaceO(1)pointer only
Current:
Current
LCA / Path
p, q
Current
Ready
Press Play. While both p,q on same side of root: root = root.left or root.right. Else root is LCA.