In a tree, the lowest common ancestor (LCA) of two nodes, n1 and n2, is the shared ancestor located farthest from the root and has both n1 and n2 as descendants.
The Lowest Common Ancestor (LCA) concept has various applications:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
public class LowestCommonAncestor {
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = findLowestCommonAncestor(root.left, p, q);
TreeNode right = findLowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
return root;
} else if (left != null) {
return left;
} else {
return right;
}
}
public static void main(String[] args) {
// Create the tree:
// 3
// / \
// 5 1
// / \ / \
// 6 2 0 8
// / \
// 7 4
TreeNode root = new TreeNode(3);
root.left = new TreeNode(5);
root.right = new TreeNode(1);
root.left.left = new TreeNode(6);
root.left.right = new TreeNode(2);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(8);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);
TreeNode n1 = root.left; // Node with value 5
TreeNode n2 = root.right; // Node with value 1
LowestCommonAncestor lca = new LowestCommonAncestor();
TreeNode result = lca.findLowestCommonAncestor(root, n1, n2);
System.out.println(result.val); // Output: 3
}
}
In this Java implementation, we have a TreeNode
class to represent the nodes of the tree. The findLowestCommonAncestor
method recursively searches for the LCA in the left and right subtrees, following the steps mentioned in the recursive approach. The main
method demonstrates the usage by creating a tree and finding the LCA of two nodes.
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode parent;
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
this.parent = null;
}
}
public class LowestCommonAncestor {
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// Assign parent pointers
assignParentPointers(root, null);
Set<TreeNode> visited = new HashSet<>();
// Traverse from n1 towards the root, marking visited nodes
while (p != null) {
visited.add(p);
p = p.parent;
}
// Traverse from n2 towards the root, checking visited nodes
while (q != null) {
if (visited.contains(q)) {
return q;
}
q = q.parent;
}
return null;
}
private void assignParentPointers(TreeNode node, TreeNode parent) {
if (node != null) {
node.parent = parent;
assignParentPointers(node.left, node);
assignParentPointers(node.right, node);
}
}
public static void main(String[] args) {
// Create the tree:
// 3
// / \
// 5 1
// / \ / \
// 6 2 0 8
// / \
// 7 4
TreeNode root = new TreeNode(3);
root.left = new TreeNode(5);
root.right = new TreeNode(1);
root.left.left = new TreeNode(6);
root.left.right = new TreeNode(2);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(8);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);
TreeNode n1 = root.left; // Node with value 5
TreeNode n2 = root.right; // Node with value 1
LowestCommonAncestor lca = new LowestCommonAncestor();
TreeNode result = lca.findLowestCommonAncestor(root, n1, n2);
System.out.println(result.val); // Output: 3
}
}
In this Java implementation, we have a TreeNode
class to represent the nodes of the tree. The findLowestCommonAncestor
method implements the iterative approach by assigning parent pointers to each node in the tree and then traversing from the given nodes (n1
and n2
) towards the root, using the parent pointers. The first common visited node encountered is the Lowest Common Ancestor (LCA). The main
method demonstrates the usage by creating a tree and finding the LCA of two nodes.
n1
and n2
) to the root. Each traversal takes at most O(N) time, as it visits each node at most once. Therefore, the overall time complexity is linear with respect to the number of nodes.Note: also read about Binary Search Tree (BST)
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…