Dodomeki
Sign in

73. Lowest Common Ancestor in a BST

Medium

In a binary search tree, receive the root and two values that exist in it. Return the value of the lowest common ancestor of these nodes, counting a node itself as its own ancestor.

Example 1
Input: raiz = [8, 3, 12, 1, 5, 10, 14], a = 1, b = 5
Output: 3
Example 2
Input: raiz = [8, 3, 12], a = 3, b = 12
Output: 8
Constraints
  • A entrada é uma BST válida.
  • Os dois valores são distintos e existem na árvore.
TreesBinary search treeDFS
Caso 1
raiz =
[8,3,12,1,5,10,14]
a =
1
b =
5
Expected =
3
Caso 2
raiz =
[8,3,12]
a =
3
b =
12
Expected =
8

Run checks these 2 cases. Submit runs all 6, including hidden ones.