Lowest Common Ancestor in a Binary Search Tree
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Implement `lowest_common_ancestor(level_order, p, q)` for a binary search tree with unique integer values. Return the integer value of the deepest node whose subtree contains both target nodes. A node belongs to its own subtree, so either target can be the answer.
`level_order` encodes the tree in breadth-first order. Its first value is the root. For each non-null node, consume its next left and right child entries; null entries have no children. Omitted trailing children are null. Examples use `null` for an absent child.
The input is a valid BST with 2 through 100,000 nodes. Values are between -1,000,000,000 and 1,000,000,000. The distinct values `p` and `q` both exist. Every left-subtree value is smaller than its ancestor; every right-subtree value is larger.
### Examples
```text
lowest_common_ancestor([6,2,8,0,4,7,9,null,null,3,5], 2, 8) -> 6
lowest_common_ancestor([6,2,8,0,4,7,9,null,null,3,5], 2, 4) -> 2
```
The second result is a target node because its subtree also contains the other target. Return a value rather than a tree-node object; uniqueness makes the answer unambiguous.
Problem reference: [LeetCode 235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/).
Overview: Find the lowest common ancestor of two nodes in a binary search tree, with a precise level-order input format and ancestor-as-target edge semantics.
Implement `lowest_common_ancestor(level_order, p, q)` for a binary search tree with unique integer values. Return the integer value of the deepest node whose subtree contains both target nodes. A node belongs to its own subtree, so either target can be the answer.
`level_order` encodes the tree in breadth-first order. Its first value is the root. For each non-null node, consume its next left and right child entries; null entries have no children. Omitted trailing children are null. Examples use `null` for an absent child.
The input is a valid BST with 2 through 100,000 nodes. Values are between -1,000,000,000 and 1,000,000,000. The distinct values `p` and `q` both exist. Every left-subtree value is smaller than its ancestor; every right-subtree value is larger.
### Examples
```text
lowest_common_ancestor([6,2,8,0,4,7,9,null,null,3,5], 2, 8) -> 6
lowest_common_ancestor([6,2,8,0,4,7,9,null,null,3,5], 2, 4) -> 2
```
The second result is a target node because its subtree also contains the other target. Return a value rather than a tree-node object; uniqueness makes the answer unambiguous.
Problem reference: [LeetCode 235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/).
Constraints
- The valid BST has 2 through 100000 non-null nodes with unique values.
- Every value, including p and q, is between -1000000000 and 1000000000.
- p and q are distinct values that both exist in the tree.
- Consume up to two child entries for each non-null node in breadth-first order; null entries have no children, and omitted trailing children are null.
- Return the ancestor value; a target belongs to its own subtree.
Examples
Input: ([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 2, 8)
Expected Output: 6
Explanation: The source example splits at the root.
Input: ([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 2, 4)
Expected Output: 2
Explanation: The second example has one target as the ancestor.
Hints
- Null entries do not consume their own child entries.
- Either target may itself be the returned ancestor.