Question: Write a program in Java to determine the lowest common ancestor of two nodes in the following binary search tree, which you may hard code

Write a program in Java to determine the lowest common ancestor of two nodes in the following binary search tree, which you may hard code in your program:

 30 | --+-- | | 8 52 | --+-- | | 3 20 | --+-- | | 10 29 

Input:

Your program should read one line of text from standard input. The line will contain two integers, separated by a space, which represent two nodes within the pictured binary search tree.

Output:

Print to standard output the least common ancestor of the two nodes.

Test 1

Test Input

8 52 

Expected Output

30 

Test 2

Test Input

3 29 

Expected Output

8 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!