Lowest Common Ancestor in a BST | Practice | GeeksforGeeks

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

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 <= N <= 104
0
sikkusaurav1236 days ago
gud Q.
Node* LCA(Node *root, int n1, int n2){  //Your code here  if(root==NULL)  {      return NULL;  }  if(root->data==n1 || root->data==n2)  {      return root;  }   Node* left=LCA(root->left,n1,n2);   Node* right=LCA(root->right,n1,n2);  if(left!=NULL && right!=NULL)  {      return root;  }  else if(left!=NULL && right==NULL)  {      return left;  }  else  {      return right;  }  }
 
0
adarshgupta4012 weeks ago
5 line Easy Java Code-
Node LCA(Node root, int p, int q)
	{
        if(root == null) return null;
        int cur = root.data;
        if(cur> p && cur> q) return LCA(root.left, p, q);
        if(cur< p && cur< q) return LCA(root.right, p, q);
        return root;        
    }
0
deepeshupadhyay853 weeks ago
Java Solution 
 
class BST{      //Function to find the lowest common ancestor in a BST. Node LCA(Node node, int d1, int d2){       // code here.       if (node == null) {           return null;       }       if(d1<node.data && d2<node.data){           return LCA(node.left,d1,d2);       }else if(d1>node.data && d2 > node.data){           return LCA(node.right,d1,d2);       }else{           return node;       }   }   } 
+1
aryankhatana353 weeks ago
   if(root==NULL){     return NULL; } if(root->data>n1 && root->data>n2){     return LCA(root->left,n1,n2); } if(root->data<n1 && root->data<n2){     return LCA(root->right,n1,n2); } return root;
0
aryankhatana353 weeks ago
   
while(root!=NULL){      if(root->data>n1 && root->data>n2)            root=root->left;      else if(root->data<n1 && root->data<n2 )            root=root->right;      else{          return root;      }           }
+2
aryanar2018813 weeks ago
C++ Code 
Node* LCA(Node *root, int n1, int n2){   Node *temp = root;   while(true)   {       if(!temp) return NULL;       if(n1==temp->data) return temp;       else if(n2==temp->data) return temp;       else if(temp->data>n1 && temp->data<n2) return temp;       else if(temp->data<n1 && temp->data>n2) return temp;       else if(temp->data>n1 && temp->data>n2) temp = temp->left;       else if(temp->data<n1 && temp->data<n2) temp = temp->right;   }}
0
kritikasinha2564 weeks ago
PYTHON 
def LCA(root, n1, n2):   l, r= min(n1, n2), max(n1,n2)      while root is not None:       if l <= root.data <= r:           return root       elif root.data >l and root.data>r:           root = root.left       else:           root = root.right
+1
sfazal0001 month ago
node* LCA(node* root, int n1 ,int n2){   if(root == NULL)   {       return NULL;   }      if(n1 < root->data and n2 < root->data)    {        return LCA(root->left, n1 , n2);    }    else if(n1 > root->data and n2 > root ->data)     {         return LCA(root->right , n1 ,n2);     }     else     {         return root;     }}         
+2
adityasingh1091 month ago
Node* LCA(Node *root, int n1, int n2)
{
    if(root == NULL)
        return NULL;
        
    if(root->data < n1 && root->data <n2)
        return LCA(root->right, n1,n2);
        
    if(root->data > n1 && root->data >n2)
        return LCA(root->left, n1,n2);
        
    return root;    
}
0
detroix071 month ago
Node* LCA(Node *root, int n1, int n2){  if(root==NULL) return NULL;  if(root->data==n1 or root->data==n2) return root;  Node* left = LCA(root->left,n1,n2);  Node* right = LCA(root->right,n1,n2);  if(left!=NULL and right!=NULL) return root;  else if (left==NULL) return r;  else if(right==NULL) return l;} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.