Lowest Common Ancestor of a Binary Search Tree
Find the deepest node that has both given nodes below it, using the BST order to pick a side.
The problem
You get the root of a binary search tree and two different nodes p and q that are both in the tree. Return their lowest common ancestor: the deepest node that has both p and q in its subtree. A node counts as part of its own subtree, so p or q itself can be the answer.
Input root = [20, 10, 30, 5, 15, 25, 35], p = 5, q = 15
Output 10
Both values are smaller than 20, so they sit on the left; at 10 they split, 5 to the left and 15 to the right.
Input root = [20, 10, 30, 5, 15, 25, 35], p = 30, q = 35
Output 30
35 is below 30, and a node counts as its own ancestor.
Key constraint: It is a binary search tree: every value in a left subtree is smaller than the node, and every value in a right subtree is larger.
How to spot the pattern
- The tree is a BST, so comparing values tells you which side each node is on.
- You need the point where the paths from the root to
pand toqsplit apart.
The idea
Start at the root and compare both values with the current node. If both are smaller, the split must happen further left; if both are larger, further right. The first node where they are not on the same side, or where one of them is the current node, is where the two paths split, and that node is the answer.
Brute force: Ignore the ordering and search the whole tree as if it were a plain binary tree, which takes O(n) time instead of O(h).
Step by step
- 1Start with
node = root. - 2If
pandqare both smaller thannode, move tonode.left. - 3Else if
pandqare both larger thannode, move tonode.right. - 4Otherwise return
node:pandqare on different sides, or one of them isnode.
See the pattern move
The tree dfs and bst pattern on a small example. Step through it, or press play.
Code
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
node = root
while node:
if p.val < node.val and q.val < node.val:
node = node.left # both are in the left subtree
elif p.val > node.val and q.val > node.val:
node = node.right # both are in the right subtree
else:
return node # they split here, or one of them is this node
return NoneComplexity
Time
O(h)
Space
O(1)
Each step goes one level down, so the walk is at most the tree's height h (about log n when balanced, n when skewed), with no recursion stack.
Watch out for
- Using
<=or>=in the comparisons, which walks past the answer whenporqis the current node. - Writing the general binary tree solution and missing the simpler walk that the BST order allows.
- Comparing a node with a value: compare
p.valwithnode.val, notpwithnode.val.
Follow-up questions
What if the tree is a plain binary tree, not a BST?
Search both children recursively; if p and q turn up in different subtrees, the current node is the answer. That takes O(n) time.
Can you write it recursively?
Yes, recurse left or right with the same comparisons. It is just as short, but it uses O(h) stack space.
In Hinglish
Dono values chhoti hain toh left jao, dono badi hain toh right. Jahan raaste alag ho jaayein, wahi answer hai.