Problem Description
Given a binary search tree (BST) and two nodes in the tree, p and q, find the lowest common ancestor (LCA) of those nodes. The LCA is defined as the lowest node in the tree that has both p and q as descendants (where a node can be a descendant of itself).
Key Insights
- Utilize the BST property: for any node, values in the left subtree are less and values in the right subtree are greater.
- If both p and q have values less than the current node, the LCA must lie in the left subtree.
- If both p and q have values greater than the current node, the LCA must lie in the right subtree.
- When one node value is less than and the other is greater than (or equal to) the current node, the current node is the lowest common ancestor.
- An iterative approach is efficient and uses constant extra space.
Space and Time Complexity
Time Complexity: O(h), where h is the height of the tree (O(log n) for balanced trees and O(n) in the worst-case skewed tree).
Space Complexity: O(1) with an iterative approach (O(h) if using recursion due to call stack).
Solution
The solution leverages the BST properties.
Algorithm:
- Start at the root node.
- While the current node exists:
- If both p and q have values less than the current node’s value, move to the left child.
- If both p and q have values greater than the current node’s value, move to the right child.
- Otherwise, the current node is the split point where p and q diverge, so it is the lowest common ancestor.
- Return the current node.
This approach efficiently finds the LCA using a single traversal from the root.