-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_160_LCA.java
165 lines (125 loc) · 3.79 KB
/
a_160_LCA.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.ArrayList;
public class a_160_LCA {
static class Node{
int data ;
Node left , right ;
Node(int data ) {
this.data = data ;
this.left = null ;
this.right = null ;
}
}
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 ;
}
// Approach : 1
public static Node lca(Node root, int n1, int n2){
ArrayList<Node> path1 = new ArrayList<>() ;
ArrayList<Node> path2 = new ArrayList<>() ;
getPath(root, n1, path1) ;
getPath(root, n2, path2) ;
// last common ancestor
int i=0 ;
for(; i<path1.size() && i<path2.size(); i++){
if(path1.get(i) != path2.get(i)){
break ;
}
}
// last equal node -> (i-1)th
Node lca = path1.get(i-1) ;
return lca ;
}
// Approach : 2
public static Node lca2(Node root, int n1, int n2){
if(root == null || root.data == n1 || root.data == n2){
return root ;
}
Node leftLCA = lca2(root.left, n1, n2) ;
Node rightLCA = lca2(root.right, n1, n2) ;
// leftLCA = values and rightLCA = null
if(rightLCA == null){
return leftLCA ;
}
if(leftLCA == null){
return rightLCA ;
}
return root ;
}
public static int lcaDist(Node root, int n){
if(root == null){
return -1 ;
}
if(root.data == n){
return 0 ;
}
int leftDist = lcaDist(root.left, n) ;
int rightDist = lcaDist(root.right, n) ;
if(leftDist == -1 && rightDist == -1){
return -1 ;
}
else if(leftDist == -1){
return rightDist + 1 ;
}
else{
return leftDist + 1 ;
}
}
public static int minDist(Node root, int n1, int n2){
Node lca = lca2(root, n1, n2) ;
int dist1 = lcaDist(lca, n1) ;
int dist2 = lcaDist(lca, n2) ;
return dist1 + dist2 ;
}
public static int KAncestor(Node root , int n, int k){
if(root == null){
return -1 ;
}
if(root.data == n){
return 0 ;
}
int leftDist = KAncestor(root.left, n, k) ;
int rightDist = KAncestor(root.right, n, k) ;
if(leftDist == -1 && rightDist == -1){
return -1 ;
}
int max = Math.max(leftDist, rightDist) ;
if(max+1 == k){
System.out.println(root.data);
}
return max + 1 ;
}
public static void main(String[] args) {
// Q.1 : Last common Ancestor
// Q.2 : Minimum distance of 2 node
// Q.3 : Kth Ancestor of node
/*
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(6) ;
root.right.right = new Node(7) ;
int n = 7, k = 2 ;
KAncestor(root , n, k) ;
}
}