-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowestCommonAncestor.java
83 lines (63 loc) · 1.91 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
79
80
81
82
83
package Binary_Tree;
import java.util.*;
public class LowestCommonAncestor {
static class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
public static boolean getPath(Node root, Node p, ArrayList path){
if (root == null) return false;
path.add(root);
if(root.data == p.data){
return true;
}
boolean left = getPath(root.left, p, path);
boolean right = getPath(root.right, p, path);
if (left || right){
return true;
}
// agar nhi mila toh remove krdo aur false return krdo
path.remove(path.size()-1);
return false;
}
public static Node lca(Node root, Node p, Node q){
if (root == null) return null;
// 1. Get the path
ArrayList<Node> path1 = new ArrayList<>();
ArrayList<Node> path2 = new ArrayList<>();
getPath(root, p, path1);
getPath(root, q, path2);
int i;
for (i=0; i < path1.size() && i < path2.size(); i++){
if (path1.get(i) != path2.get(i)){
break;
}
}
Node lca = path1.get(i-1);
return lca;
}
public static void main(String[] args) {
/*
1
/ \
2 3
/ \ / \
4 5 6 7
*/
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.left = new Node(7);
root.right.right = new Node(6);
Node ans = lca(root, root.left.left, root.left.right);
System.out.println(ans.data);
}
}