-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLowestCommonAncestor.java
78 lines (71 loc) · 2.02 KB
/
LowestCommonAncestor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package SummerTrainingGFG.BinarySearchTree;
/**
* @author Vishal Singh
* 25-12-2020
*/
public class LowestCommonAncestor {
static class Node{
int data;
Node left,right;
Node(int data){
this.data = data;
}
}
Node root;
/*
* This is the recursice approach
* */
Node lca(Node root,int n1,int n2){
if (root == null){
return null;
}
if (root.data > n1 && root.data > n2){
return lca(root.left,n1,n2);
}
else if (root.data < n1 && root.data < n2){
return lca(root.right,n1,n1);
}else {
return root;
}
}
/*
* This is the alternate iterative approach. Much Efficient
* */
Node lcaAlternate(Node root,int n1,int n2){
if (root == null){
return null;
}
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 {
break;
}
}
return root;
}
public static void main(String[] args) {
LowestCommonAncestor tree = new LowestCommonAncestor();
tree.root = new Node(20);
tree.root.left = new Node(8);
tree.root.right = new Node(22);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(12);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(14);
int n1 = 10, n2 = 14;
Node t = tree.lca(tree.root, n1, n2);
System.out.println("LCA of " + n1 + " and " + n2 + " is " + t.data);
n1 = 14;
n2 = 8;
t = tree.lca(tree.root, n1, n2);
System.out.println("LCA of " + n1 + " and " + n2 + " is " + t.data);
n1 = 10;
n2 = 22;
t = tree.lcaAlternate(tree.root, n1, n2);
System.out.println("LCA of " + n1 + " and " + n2 + " is " + t.data);
}
}