← Whiteboard questions

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

Input tree = [40, 25, 60, 10, 30, 50, 80, null, 15, 27], p = 15, q = 27
Output 25
Why node 25 has 15 below it on the left and 27 below it on the right.
Input tree = [40, 25, 60, 10, 30, 50, 80, null, 15, 27], p = 30, q = 27
Output 30
Why node 27 is below node 30, and a node counts as below itself.
Input tree = [40, 25, 60, 10, 30, 50, 80, null, 15, 27], p = 50, q = 50
Output 50
Why when both values are the same node, that node is the answer.

Constraints

Your answer must contain

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.val

Time 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.

Related problems

More Trees problems
Lowest Common Ancestor of a BST: walkthrough and solution