Challenge: Least Common Ancestor

Let’s find the least common ancestor between two nodes in a BST.

Problem

In a BST, find the least common ancestor (LCA) between the two given nodes.

Input

Two node values.

Output

Return the LCA and true if the tree is not empty. Otherwise, return false.

Sample input

n1 = 3
n2 = 4

Sample output

4 true

Let’s look at the illustration to better understand the problem.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.