Thursday, January 14, 2016

Binary Search Tree : Lowest Common Ancestor - Hacker Rank Solution

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. You only need to complete the function.

 Binary Search Tree : Lowest Common Ancestor - Hacker Rank Solution

Input Format
You are given a function,
node * LCA (node * root ,int v1,int v2)
{

}
It is guaranteed that v1 and v2 are present in the tree.
Node is defined as :
struct node
{
int data;
node * left;
node * right;
}node;
Output Format
Return the LCA of v1 and v2.
Sample Input
         4
       /   \
      2     7
     / \   /
    1   3 6
v1=1 and v2=7.
Sample Output
LCA of 1 and 7 is 4 (which is the root).
Return a pointer to the root in this case.
------------------------------------------------------------------------------------------------------------

 Binary Search Tree : Lowest Common Ancestor - code

/*
Node is defined as

typedef struct node
{
   int data;
   node * left;
   node * right;
}node;

*/


node * lca(node * root, int v1,int v2)
{
    if(root==NULL)
        return NULL;
    if(root->data > v1 && root->data > v2)
        return lca(root->left,v1,v2);
    if(root->data < v1 && root->data < v2)
        return lca(root->right,v1,v2);
       
        return root;
       


  
}

---------------------------------------------------------------------------------------------------------- 
 Binary Search Tree : Lowest Common Ancestor - Hacker Rank Solution
Powered by Blogger.