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

Write a program 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 Download Test Input 3 29

Expected Output Download Test Output 8

This is the starter code I've been given:

public class MainFour {

/**

* Iterate through each line of input.

*/

public static void main(String[] args) throws IOException {

InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);

BufferedReader in = new BufferedReader(reader);

String line;

while ((line = in.readLine()) != null) {

System.out.println(line);

}

}

}

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!