Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Lowest Common Ancestor of a Binary Search Tree

XPChallenge Points: 20
levelLevel: Medium

Given a binary search tree (BST), find the lowest common parent node of two given nodes.
The lowest common parent is the deepest node that has both given nodes as children (a node can be a child of itself).

Example 1:

Input: root = [10,5,15,2,7,12,20], p = 5, q = 15

Output: 10

Explanation:

The lowest common parent of nodes 5 and 15 is 10.

Example 2:

Input: root = [10,5,15,2,7,12,20], p = 2, q = 7

Output: 5

Explanation:

Node 5 is the parent of both 2 and 7.

Example 3:

Input: root = [3,1,4,null,2], p = 1, q = 2

Output: 1

Explanation:

Node 1 is the parent of node 2 (a node can be its own descendant).

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result