Timer is Running

Given a Binary Search Tree (with all values unique) and two node values. Find the Lowest Common Ancestors of the two nodes in the BST.

Input:
First line of input contains the number of test cases T. For each test case, there will be two line of input. First line is a string representing the tree as described below:

  1. The values in the string are in the order of level order traversal of the tree where, numbers denotes node values, and a character “N” denotes NULL child.

  2. For example:

    For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N

  3. The second line of Input contains two space separated integer values n1 and n2.

Output:
For each testcase, in a new line, print the LCA of n1 and n2.

Your Task:
You don't need to read input or print anything. Your task is to complete the function LCA() which takes the root Node of the BST and two integer values n1 and n2 as inputs and returns the Lowest Common Ancestor of the Nodes with values n1 and n2 in the given BST. 

Expected Time Complexity: O(Height of the BST).
Expected Auxiliary Space: O(Height of the BST).

Constraints:
1 <= T <= 100
1 <= N <= 104

Example:
Input:
2
5 4 6 3 N N 7 N N N 8
7 8
2 1 3
1 3

Output:
7
2

Explanation:
Testcase1: 
The BST in above test case will look like
              5
          /        \ 
        4          6
     /     \     /      \
   3      N  N       7
 /    \                 /    \ 
N   N              N     8
Here, the LCA of 7 and 8 is 7.
Testcase 2: For the given above test case the BST will be
                    2
                  /    \
                1      3
Here, the LCA of 1 and 3 will be 2

 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal, Harshit Sidhwa
Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.