Lowest Common Ancestor of a BST
Find the lowest common ancestor of two values in a binary search tree
DSA, Medium, Trees
Which structure or algorithm, and what does it cost.
You get a binary search tree tree. Your code may take the root node or the list. Each node has val, left and right, null when missing. Either is fine; no conversion is needed. The examples use a level-order list. It holds values row by row, left to right. Null marks a missing child. A missing node has no entries below it. Left values are smaller and right values are larger, and all values differ. The values p and q both occur in the tree, and they may be equal. Return the value of the lowest node that has both p and q in its subtree. A node counts as being in its own subtree.
Examples
Constraints
- 1 <= number of nodes <= 30,000.
- Each value is a whole number between -100,000 and 100,000, and all values in the tree are different.
- The tree can be one long chain.
Your answer must contain
- The simple method and its cost; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(h) time, where h is the height of the tree.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Start at the root. What do you learn from comparing the root with the two values?
Hint 2 the approach
If both values are on one side of a node, the answer is on that side too. When can you not go on?
Hint 3 the key step
Walk down from the root. If both values are smaller, go left. If both are larger, go right. The first node where they split, or equal one, is the answer.
Walkthrough
Full walkthrough brute force to best
Brute force
Search the whole tree for the path from the root to p, then the path from the root to q, without using the order of the values. Compare the two paths and keep the last node they share. Each search can visit every node, so this is O(n) time and O(n) memory for the paths.
Best method
In a search tree, every value in the left subtree of a node is smaller and every value in the right subtree is larger. Start at the root. If both p and q are smaller than the node, both lie in its left subtree, so the answer is there too: go left. If both are larger, go right. Otherwise the values are on different sides, or one of them is the node itself, so this node is the lowest one that has both below it. Stop and return it. The walk makes one step per level and keeps no extra data, so the cost depends on the height, not on the node count.
Hand trace
Run on [40, 25, 60, 10, 30, 50, 80, null, 15, 27] with p = 15 and q = 27. Start at 40. Both 15 and 27 are smaller than 40, so go left to 25. Now 15 is smaller than 25 but 27 is larger. They split here, so the answer is 25.
Code
def lowest_common_ancestor(root, p, q):
node = root
while True:
if p < node.val and q < node.val:
node = node.left
elif p > node.val and q > node.val:
node = node.right
else:
return node.valTime and memory
O(h) time for the walk, because it takes one step per level. That is O(log n) for a balanced tree and O(n) for a chain. O(1) extra memory, because it keeps only the current node.
Common mistakes
- Going on when a value equals the current node. If p equals the node, that node is the answer, because it counts as being below itself.
- Searching the whole tree and ignoring the order of the values. It works, but costs O(n) where the order gives O(h).
- Assuming the answer is always a node strictly above both values. It can be one of the two values, as in example 2.