Site icon

Find least or lowest common ancestor (LCA) in binary search tree (java/ example)

Fig 1: LCA in Binary search tree

Examples to find LCA of input nodes:

LCA of input nodes using recursive algorithm is as follows:

S. No Input nodes LCA
1 F & G  C
2 C &  G C
3 D & E B
4 D & B B
5 D & C A
6 E & G A

Example 1: find LCA of Node J & Node K in a binary search tree (java)

Fig 2: LCA (J, K) = Node G

Example 2: find LCA of node E & node G in BST using java

Fig 3: LCA ( E , G) = Node A

Example 3: find LCA of node G and node J in BST using java

Fig 4: LCA ( G, J) = Node G

Conclusion: LCA node will have value within (or equal to any node) range of input nodes

Algorithm: find LCA(J,K) in binary search tree using java

LCA (J, K ) = Node G,

Program: find least common ancestor in BST using recursive algorithm

1.) LCA Class:

package org.learn.Question;

import org.learn.PrepareTree.Node;

public class LCA {
	public static Node lca(Node root, Node A, Node B) {
		if (null == root) {
			return root;
		}

		if (root.data > A.data && root.data > B.data) {
			return lca(root.left, A, B);
		}

		if (root.data < A.data && root.data < B.data) {
			return lca(root.right, A, B);
		}

		return root;
	}
}

2.) Node Class :

package org.learn.PrepareTree;

public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int num) {
		this.data = num;
		this.left = null;
		this.right = null;
	}

	public Node() {
		this.left = null;
		this.right = null;
	}
	public static Node createNode(int number) {
		return new Node(number);
	}
}

3.) App: 

package org.learn.Client;

import org.learn.PrepareTree.Node;
import org.learn.Question.LCA;

public class App 
{
    public static void main( String[] args )
    {  
       //root level 0
       Node A = Node.createNode(100);
       //Level 1
       Node B = Node.createNode(50);
       Node C = Node.createNode(150);
       //Level 2
       Node D = Node.createNode(25);
       Node E = Node.createNode(75);
       Node F = Node.createNode(125);
       Node G = Node.createNode(175);
       //Level 3
       Node H = Node.createNode(120);
       Node I = Node.createNode(140);
       Node J = Node.createNode(160);
       Node K = Node.createNode(190);
             
       //connect Level 0 and 1
       A.left = B;
       A.right = C;
       //connect level 1 and level 2
       B.left = D;
       B.right = E;
       C.left = F;
       C.right = G;
       //Connect level 2 and level 3
       F.left = H;
       F.right = I;
       G.left = J;
       G.right = K;
         
       Node ancestor = LCA.lca(A, J, K);
       System.out.printf("Ancestor of %d and %d is %dn",J.data,K.data,ancestor.data);
       
       ancestor = LCA.lca(A, J, G);
       System.out.printf("Ancestor of %d and %d is %dn",J.data,G.data,ancestor.data);
       
       ancestor = LCA.lca(A, E, G);
       System.out.printf("Ancestor of %d and %d is %dn",E.data,G.data,ancestor.data);
    }
}

Output : least common ancestors in binary search tree using java

Ancestor of 160 and 190 is 175
Ancestor of 160 and 175 is 175
Ancestor of 75 and 175 is 100

 Download Code – Least common ancestor (LCA) in BST

Exit mobile version