235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Example 1:
Example 2:
Example 3:
- The number of nodes in the tree is in the range [2, 105].
- -109 <= Node.val <= 109
- All Node.val are unique.
- p != q
- p and q will exist in the BST.
Constraints:
Notes
Intuition
In a BST, all nodes in the left subtree are smaller and all in the right are larger. The LCA is the first node where p and q diverge—one goes left, one goes right (or one equals the current node).
Implementation
Solve iteratively for better space. Start at the root and traverse down. If both p and q are greater than the current value, move right. If both are smaller, move left. Otherwise, we've found the split point—this is the LCA.
Edge-cases
The problem guarantees p and q exist in the tree, so we don't need null checks for them. One node being the ancestor of another is handled naturally—the divergence point will be that ancestor.
- Time: O(h) — traverse at most the height of the tree
- Space: O(1) — iterative approach uses no extra space