Problem Statement

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.

Example 1:

Input:
              5
           /    \\
         4       6
        /         \\
       3           7
                    \\
                     8
n1 = 7, n2 = 8
Output:7

Example 2:

Input:
     2
   /   \\
  1     3
n1 = 1, n2 = 3
Output:2

Problem Link

Lowest Common Ancestor in a BST | Practice | GeeksforGeeks

Code

//Function to find the lowest common ancestor in a BST. 
Node* LCA(Node *root, int n1, int n2)
{
   if(!root||n1==root->data||n2==root->data)
   return root;
   
   if(n1<root->data&&n2>root->data||n2<root->data&&n1>root->data)
   return root;
   
   if(n1>root->data&&n2>root->data)
   return LCA(root->right,n1,n2);
   
    return LCA(root->left,n1,n2);
}