Lowest Common Ancestor of a BST
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

30:00

Lowest Common Ancestor of a BST
medium
Topics
Companies

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

Example 1:
Input: {"root":[6,2,8,0,4,7,9,null,null,3,5],"p":2,"q":8}
Output: 6
Constraints:
  • The number of nodes is in the range [2,105][2, 10^5].

  • 109Node.val109-10^9 \leq \text{Node.val} \leq 10^9

  • All Node.val are unique.

  • p != q

  • p and q will exist in the BST.

Input
arr ={"root":[6,2,8,0,4,7,9,null,null,3,5],"p":2,"q":8}

Start at root

6

2

8

0

4

7

9

3

5

Variables
No variables to display
DepthFunction Call
Stack empty
0/4