Lowest Common Ancestor in a BST

Medium · Binary Search Trees · about 15 minutes

Asked at Meta, Amazon, Microsoft, LinkedIn.

p and q are two values stored in a binary search tree. Return the value of their lowest common ancestor: the node furthest from the root whose subtree holds both values. A subtree includes its own root, so the answer can be p or q itself.

Related guides