Lowest Common Ancestor in a Binary Search Tree

Last Updated : 1 Aug, 2026

Given the root of a Binary Search Tree (BST) with unique node values and two nodes n1 and n2 (n1 != n2), find their Lowest Common Ancestor (LCA).

The Lowest Common Ancestor (LCA) of two nodes is defined as the deepest node in the tree that has both n1 and n2 as descendants, where a node can be a descendant of itself.

Examples: 

Input:  root = [20, 8, 22, 4, 12, N, N, N, N, 10 , 14 ], n1-> data = 4, n2-> data = 14

Lowest-common-ancestor-in-a-binary-tree-1

Output: 8
Explanation: 8 is the lowest common ancestor (LCA) of nodes 4 and 14, as it is the deepest node that is an ancestor of both.

Lowest-common-ancestor-in-a-binary-tree-2

Input: root = [20, 8, 22, 4, 12, N, N, N, N, 10 , 14 ], n1-> data = 10, n2-> data = 14

Lowest-common-ancestor-in-a-binary-tree-1

Output: 12
Explanation: 12 is the lowest common ancestor (LCA) of nodes 10 and 14, as it is the deepest node that is an ancestor of both.

Lowest-common-ancestor-in-a-binary-tree-4
Try It Yourself
redirect icon

[Naive Approach] LCA by Normal Binary Tree Methods - O(n) Time and O(n) Space

We can use any of the approaches discussed in Lowest Common Ancestor in a Binary Tree, which run in O(n) time, where n is the number of nodes in the BST. However, we can achieve a better time complexity by leveraging the properties of the BST.

[Better Approach] Using BST Properties (Recursive Approach) - O(h) Time and O(h) Space

This approach takes advantage of the Binary Search Tree (BST) property to avoid searching the entire tree. Starting from the root, if both n1 and n2 have values smaller than the current node, then the LCA must be present in the left subtree. Similarly, if both values are greater than the current node, then the LCA must be present in the right subtree. Otherwise, the current node is the first node where the paths to n1 and n2 split, making it their Lowest Common Ancestor.

Consider: root = [20, 8, 22, 4, 12, N, N, N, N, 10 , 14 ], n1-> data = 10, n2-> data = 14

Lowest-common-ancestor-in-a-binary-tree-1

Start from the root (20).

  • Both 10 and 14 are smaller than 20.
  • So, the LCA cannot be in the right subtree.
  • Move to the left child (8).

At node 8.

  • Both 10 and 14 are greater than 8.
  • So, the LCA cannot be in the left subtree.
  • Move to the right child (12).

At node 12.

  • 10 is smaller than 12, while 14 is greater than 12.
  • This means one node is in the left subtree and the other is in the right subtree.

Therefore, 12 is the first node where their paths split, so it is the Lowest Common Ancestor (LCA).

C++
//Driver Code Starts
#include <iostream>
using namespace std;

// Node structure
class Node {
  public:
    int data;
    Node* left;
    Node* right;
    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};
//Driver Code Ends


Node* findLCA(Node* root, Node* n1, Node* n2) {
  	
    if (root == nullptr)
        return nullptr;

    // If both n1 and n2 are smaller than 
    // root, go to left subtree
    if (root->data > n1->data && root->data > n2->data)
        return findLCA(root->left, n1, n2);

    // If both n1 and n2 are greater than 
    // root, go to right subtree
    if (root->data < n1->data && root->data < n2->data)
        return findLCA(root->right, n1, n2);

    // If nodes n1 and n2 are on the opposite sides, 
    // then root is the LCA
    return root;
}


//Driver Code Starts
int main() {
  	
  	// Representation of input BST:
    //            20
    //           /  \
    //          8    22
    //        /   \     
    //       4    12   
    //           /   \    
    //         10    14  
    Node* root = new Node(20);
    root->left = new Node(8);
    root->right = new Node(22);
    root->left->left = new Node(4);
    root->left->right = new Node(12);
    root->left->right->left = new Node(10);
    root->left->right->right = new Node(14);

    // Node 4
    Node* n1 = root->left->left; 
    
    // Node 14
    Node* n2 = root->left->right->right; 

    Node* res = findLCA(root, n1, n2);
    cout << res->data << endl;

    return 0;
}

//Driver Code Ends
Java
//Driver Code Starts
// Node structure
class Node {
    int data;
    Node left, right;

    Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
//Driver Code Ends

    
    public static Node findLCA(Node root, Node n1, Node n2) {
        if (root == null)
            return null;

        // If both n1 and n2 are smaller than root, 
      	// go to left subtree
        if (root.data > n1.data && root.data > n2.data)
            return findLCA(root.left, n1, n2);

        // If both n1 and n2 are greater than root, 
      	// go to right subtree
        if (root.data < n1.data && root.data < n2.data)
            return findLCA(root.right, n1, n2);

        // If nodes n1 and n2 are on the opposite sides, 
  	    // root is the LCA
        return root;
    }


//Driver Code Starts
    public static void main(String[] args) {
        // Representation of input BST:
        //            20
        //           /  \
        //          8    22
        //        /   \     
        //       4    12   
        //           /   \   
        //         10    14  
        
        Node root = new Node(20);
        root.left = new Node(8);
        root.right = new Node(22);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(14);

        // Node 4
        Node n1 = root.left.left; 
        
        // Node 14
        Node n2 = root.left.right.right; 
      
        Node res = findLCA(root, n1, n2);
        System.out.println(res.data);
    }
}

//Driver Code Ends
Python
#Driver Code Starts
# Node structure
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None

#Driver Code Ends


def findLCA(root, n1, n2):
    if root is None:
        return None

    # If both n1 and n2 are smaller than root, 
    # go to left subtree
    if root.data > n1.data and root.data > n2.data:
        return findLCA(root.left, n1, n2)

    # If both n1 and n2 are greater than root, 
    # go to right subtree
    if root.data < n1.data and root.data < n2.data:
        return findLCA(root.right, n1, n2)

    # If nodes n1 and n2 are on the opposite sides, 
    # root is the LCA
    return root


#Driver Code Starts
if __name__ == "__main__":
    # Representation of input BST:
    #            20
    #           /  \
    #          8    22
    #        /   \     
    #       4    12   
    #           /   \   
    #         10    14  
    root = Node(20)
    root.left = Node(8)
    root.right = Node(22)
    root.left.left = Node(4)
    root.left.right = Node(12)
    root.left.right.left = Node(10)
    root.left.right.right = Node(14)

    # Node 4
    n1 = root.left.left  
    
    # Node 14
    n2 = root.left.right.right 
    
    res = findLCA(root, n1, n2)
    print(res.data)

#Driver Code Ends
C#
//Driver Code Starts
using System;

// Node structure
class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
    
//Driver Code Ends

    public static Node findLCA(Node root, Node n1, Node n2) {
        if (root == null)
            return null;

        // If both n1 and n2 are smaller than root,
      	// go to left subtree
        if (root.data > n1.data && root.data > n2.data)
            return findLCA(root.left, n1, n2);

        // If both n1 and n2 are greater than root, 
      	// go to right subtree
        if (root.data < n1.data && root.data < n2.data)
            return findLCA(root.right, n1, n2);

        // If nodes n1 and n2 are on the opposite sides, 
      	// root is the LCA
        return root;
    }

//Driver Code Starts

    public static void Main(string[] args) {
        // Representation of input BST:
        //            20
        //           /  \
        //          8    22
        //        /   \     
        //       4    12   
        //           /   \   
        //         10    14  
        Node root = new Node(20);
        root.left = new Node(8);
        root.right = new Node(22);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(14);

        // Node 4
        Node n1 = root.left.left; 
        
        // Node 14
        Node n2 = root.left.right.right; 
      
        Node res = findLCA(root, n1, n2);
        Console.WriteLine(res.data);
    }
}

//Driver Code Ends
JavaScript
//Driver Code Starts
// Node structure
class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}
//Driver Code Ends


function findLCA(root, n1, n2) {
    if (root === null)
        return null;

    // If both n1 and n2 are smaller than root, go to left subtree
    if (root.data > n1.data && root.data > n2.data)
        return findLCA(root.left, n1, n2);

    // If both n1 and n2 are greater than root, go to right subtree
    if (root.data < n1.data && root.data < n2.data)
        return findLCA(root.right, n1, n2);

    // If nodes n1 and n2 are on the opposite sides, root is the LCA
    return root;
}


//Driver Code Starts
// Driver Code

// Representation of input BST:
//            20
//           /  \
//          8    22
//        /   \     
//       4    12   
//           /   \   
//         10    14  
const root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.left.right = new Node(12);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);

// Node 4
const n1 = root.left.left; 

// Node 14
const n2 = root.left.right.right;

const res = findLCA(root, n1, n2);
console.log(res.data);

//Driver Code Ends

Output
8

[Expected Approach] Using BST Properties (Iterative Method) - O(h) Time and O(1) Space

The recursive approach can be optimized by eliminating the recursion stack. Instead of making recursive calls, we iteratively traverse the BST from the root. At each node, if both n1 and n2 are smaller than the current node, we move to the left child. If both are greater, we move to the right child. Otherwise, the current node is the first node where the paths to n1 and n2 diverge, making it their Lowest Common Ancestor.

C++
//Driver Code Starts
#include <iostream>
using namespace std;

// Node structure
class Node {
  public:
    int data;
    Node* left;
    Node* right;
    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};
//Driver Code Ends


Node* findLCA(Node* root, Node* n1, Node* n2) {

    while (root != nullptr) {
      
        // If both n1 and n2 are smaller than root,
        // then LCA lies in left
        if (root->data > n1->data && root->data > n2->data)
            root = root->left;

        // If both n1 and n2 are greater than root,
        // then LCA lies in right
        else if (root->data < n1->data && root->data < n2->data)
            root = root->right;
        
        // Else Ancestor is found
        else
            break;
    }

    return root;
}


//Driver Code Starts
int main() {
  	
  	// Representation of input BST:
    //            20
    //           /  \
    //          8    22
    //        /   \     
    //       4    12   
    //           /   \   
    //         10    14  
    Node* root = new Node(20);
    root->left = new Node(8);
    root->right = new Node(22);
    root->left->left = new Node(4);
    root->left->right = new Node(12);
    root->left->right->left = new Node(10);
    root->left->right->right = new Node(14);

    // Node 4
    Node* n1 = root->left->left; 
    
    // Node 14
    Node* n2 = root->left->right->right; 

    Node* res = findLCA(root, n1, n2);
    cout << res->data << endl;

    return 0;
}

//Driver Code Ends
Java
//Driver Code Starts
// Node structure
class Node {
    int data;
    Node left, right;

    Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
//Driver Code Ends

    
    static Node findLCA(Node root, Node n1, Node n2) {
      
        while (root != null) {
          
            // If both n1 and n2 are smaller than root,
            // then LCA lies in left
            if (root.data > n1.data && root.data > n2.data)
                root = root.left;

            // If both n1 and n2 are greater than root,
            // then LCA lies in right
            else if (root.data < n1.data && root.data < n2.data)
                root = root.right;

            // Else Ancestor is found
            else
                break;
        }
      
        return root;
    }

//Driver Code Starts

    public static void main(String[] args) {
        // Representation of input BST:
        //            20
        //           /  \
        //          8    22
        //        /   \
        //       4    12
        //           /   \
        //         10    14
        
        Node root = new Node(20);
        root.left = new Node(8);
        root.right = new Node(22);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(14);

        // Node 4
        Node n1 = root.left.left; 
        
        // Node 14
        Node n2 = root.left.right.right; 

        Node res = findLCA(root, n1, n2);
        System.out.println(res.data);
    }
}

//Driver Code Ends
Python
#Driver Code Starts
# Node structure
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

#Driver Code Ends


def findLCA(root, n1, n2):
  
    while root:
      
        # If both n1 and n2 are smaller than root,
        # then LCA lies in left
        if root.data > n1.data and root.data > n2.data:
            root = root.left
            
        # If both n1 and n2 are greater than root,
        # then LCA lies in right
        elif root.data < n1.data and root.data < n2.data:
            root = root.right
            
        # Else Ancestor is found
        else:
            break
            
    return root


#Driver Code Starts
if __name__ == "__main__":
    
    # Representation of input BST:
    #            20
    #           /  \
    #          8    22
    #        /   \     
    #       4    12   
    #           /   \   
    #         10    14  
    root = Node(20)
    root.left = Node(8)
    root.right = Node(22)
    root.left.left = Node(4)
    root.left.right = Node(12)
    root.left.right.left = Node(10)
    root.left.right.right = Node(14)

     # Node 4
    n1 = root.left.left 
    
     # Node 14
    n2 = root.left.right.right

    res = findLCA(root, n1, n2)
    print(res.data)

#Driver Code Ends
C#
//Driver Code Starts
using System;

// Node structure
class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
//Driver Code Ends


    static Node findLCA(Node root, Node n1, Node n2) {
      
        while (root != null) {
          
            // If both n1 and n2 are smaller than root,
            // then LCA lies in left
            if (root.data > n1.data && root.data > n2.data)
                root = root.left;
          	
            // If both n1 and n2 are greater than root,
            // then LCA lies in right
          	
            else if (root.data < n1.data && root.data < n2.data)
                root = root.right;
          	
            // Else Ancestor is found
            else
                break;
        }
      	
        return root;
    }


//Driver Code Starts
    static void Main(string[] args) {
        // Representation of input BST:
        //            20
        //           /  \
        //          8    22
        //        /   \     
        //       4    12   
        //           /   \   
        //         10    14  
        Node root = new Node(20);
        root.left = new Node(8);
        root.right = new Node(22);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(14);

        // Node 4
        Node n1 = root.left.left; 
        
        // Node 14
        Node n2 = root.left.right.right; 
        
        Node res = findLCA(root, n1, n2);
        Console.WriteLine(res.data);
    }
}

//Driver Code Ends
JavaScript
//Driver Code Starts
// Node structure
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
//Driver Code Ends


function findLCA(root, n1, n2) {

    while (root !== null) {
    
        // If both n1 and n2 are smaller than root,
        // then LCA lies in left
        if (root.data > n1.data && root.data > n2.data) 
            root = root.left;
        
        // If both n1 and n2 are greater than root,
        // then LCA lies in right
        else if (root.data < n1.data && root.data < n2.data) 
            root = root.right;
        
        // Else Ancestor is found
        else 
            break;
    }
    return root;
}


//Driver Code Starts
// Representation of input BST:
//            20
//           /  \
//          8    22
//        /   \     
//       4    12   
//           /   \   
//         10    14  
const root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.left.right = new Node(12);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);

// Node 4
const n1 = root.left.left; 

// Node 14
const n2 = root.left.right.right; 

const res = findLCA(root, n1, n2);
console.log(res.data);

//Driver Code Ends

Output
8

Related Articles:
LCA using Parent Pointer
Find LCA in Binary Tree using RMQ

Comment