-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCAProb.java
More file actions
94 lines (78 loc) · 2.35 KB
/
LCAProb.java
File metadata and controls
94 lines (78 loc) · 2.35 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.ArrayList;
public class LCAProb {
static class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
Node lca = findLCA(root, 4, 5);
if (lca != null) {
System.out.println("LCA of 4 and 5 is: " + lca.data);
} else {
System.out.println("LCA not found.");
}
Node lca2 = lca(root, 4,5);
if (lca2 != null) {
System.out.println("LCA2 of 4 and 5 is: " + lca2.data);
} else {
System.out.println("LCA2 not found.");
}
}
private static LCAProb.Node findLCA(LCAProb.Node root, int i, int j) {
if (root == null) {
return null;
}
if (root.data == i || root.data == j) {
return root;
}
Node leftLCA = findLCA(root.left, i, j);
Node rightLCA = findLCA(root.right, i, j);
if (leftLCA != null && rightLCA != null) {
return root;
}
return (leftLCA != null) ? leftLCA : rightLCA;
}
public static boolean getPath(Node root, int n, ArrayList<Node> path){
if(root == null){
return false;
}
path.add(root);
if(root.data == n){
return true;
}
boolean foundLeft = getPath(root.left, n, path);
boolean foundRight = getPath(root.right, n, path);
if(foundLeft || foundRight){
return true;
}
path.remove(path.size()-1);
return false;
}
public static Node lca(Node root, int n1, int n2){
System.out.println("---LCA 2nd called---");
ArrayList<Node> path1 = new ArrayList<>();
ArrayList<Node> path2 = new ArrayList<>();
getPath(root, n1, path1);
getPath(root, n2, path2);
int i=0;
for(; i<path1.size() && i<path2.size(); i++){
if(path1.get(i) != path2.get(i)){
break;
}
}
Node lca = path1.get(i-1);
return lca;
}
}