Last active
November 30, 2017 03:47
-
-
Save anil477/d8079bb1defbea23b56f4acbb2768884 to your computer and use it in GitHub Desktop.
Java implementation to find lowest common ancestor of in a Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java implementation to find lowest common ancestor of | |
// n1 and n2 using one traversal of binary tree | |
// It also handles cases even when n1 and n2 are not there in Tree | |
// It handles the case when both the nodes are present in same subtree(see the 2,4 example below) | |
// The basic idea is to start searching from the lead node. | |
class Node | |
{ | |
int data; | |
Node left, right; | |
public Node(int item) | |
{ | |
data = item; | |
left = right = null; | |
} | |
} | |
public class BinaryTree | |
{ | |
// Root of the Binary Tree | |
Node root; | |
static boolean v1 = false, v2 = false; | |
// This function returns pointer to LCA of two given | |
// values n1 and n2. | |
// v1 is set as true by this function if n1 is found | |
// v2 is set as true by this function if n2 is found | |
Node findLCAUtil(Node node, int n1, int n2) | |
{ | |
// Base case | |
if (node == null) | |
return null; | |
// Look for keys in left and right subtrees | |
Node left_lca = findLCAUtil(node.left, n1, n2); | |
Node right_lca = findLCAUtil(node.right, n1, n2); | |
// If either n1 or n2 matches with root's key, report the presence | |
// by setting v1 or v2 as true and return root (Note that if a key | |
// is ancestor of other, then the ancestor key becomes LCA) | |
if (node.data == n1) | |
{ | |
v1 = true; | |
return node; | |
} | |
if (node.data == n2) | |
{ | |
v2 = true; | |
return node; | |
} | |
// If both of the above calls return Non-NULL, then one key | |
// is present in once subtree and other is present in other, | |
// So this node is the LCA | |
if (left_lca != null && right_lca != null) | |
return node; | |
// Otherwise check if left subtree or right subtree is LCA | |
return (left_lca != null) ? left_lca : right_lca; | |
} | |
// Finds lca of n1 and n2 under the subtree rooted with 'node' | |
Node findLCA(int n1, int n2) | |
{ | |
// Initialize n1 and n2 as not visited | |
v1 = false; | |
v2 = false; | |
// Find lca of n1 and n2 using the technique discussed above | |
Node lca = findLCAUtil(root, n1, n2); | |
// Return LCA only if both n1 and n2 are present in tree | |
if (v1 && v2) | |
return lca; | |
// Else return NULL | |
return null; | |
} | |
/* Driver program to test above functions */ | |
public static void main(String args[]) | |
{ | |
BinaryTree tree = new BinaryTree(); | |
tree.root = new Node(1); | |
tree.root.left = new Node(2); | |
tree.root.right = new Node(3); | |
tree.root.left.left = new Node(4); | |
tree.root.left.right = new Node(5); | |
tree.root.right.left = new Node(6); | |
tree.root.right.right = new Node(7); | |
Node lca = tree.findLCA(4, 2); | |
if (lca != null) | |
System.out.println("LCA(4, 2) = " + lca.data); | |
else | |
System.out.println("Keys are not present"); | |
lca = tree.findLCA(10, 4); | |
if (lca != null) | |
System.out.println("LCA(2, 4) = " + lca.data); | |
else | |
System.out.println("Keys are not present"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment