Dashboard
LC #235
Lowest Common Ancestor of BST
Pattern · ?AmazonMetaMicrosoftGoogle
Problem
Find the lowest common ancestor (LCA) of two nodes p and q in a BST. The LCA is the lowest node that has both p and q as descendants.
Constraints
- 2 ≤ number of nodes ≤ 10⁵
- -10⁹ ≤ node.val ≤ 10⁹
- p and q exist in the BST
- p ≠ q
Example
- Input
- root = [6,2,8,0,4,7,9], p = 2, q = 8
- Output
- 6
- Why
- LCA of 2 and 8 is 6 (root) since one is in left subtree, other in right
Spot the pattern
Which pattern cracks it?
Hints
One at a time