-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinary Search Tree : Lowest Common Ancestor.java
73 lines (56 loc) · 1.85 KB
/
Binary Search Tree : Lowest Common Ancestor.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
/*
problem link:https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem
problem name: Binary Search Tree : Lowest Common Ancestor
status:accpted ;
author : mohand sakr;
*/
/*
class Node
int data;
Node left;
Node right;
*/
static int arr[]= new int[26];
public static List<Node> findValuePath(Node root,int target){
List<Node> data=new ArrayList<Node>();
while(root.data!=target){
if(root.data>target){
root=root.left;
data.add(root);
++arr[root.data];
}
else if(root.data<target){
root=root.right;
data.add(root);
++arr[root.data];
}
}
return data;
}
public static Node lca(Node root, int v1, int v2) {
List<Node> v1Data=findValuePath(root,v1);
List<Node> v2Data=findValuePath(root,v2);
int minsize=Math.min(v1Data.size(),v2Data.size());
Node answer=root;
if(v1Data.size()>v2Data.size()) {
int dataSize=v1Data.size();
for(int i=dataSize-1;i>=0;i--){
Integer v1ParentValue=v1Data.get(i).data;
if(arr[v1ParentValue]==2){
answer= v1Data.get(i);
break;
}
}
}
else {
int dataSize=v2Data.size();
for(int i=dataSize-1;i>=0;i--){
Integer v2ParentValue=v2Data.get(i).data;
if(arr[v2ParentValue]==2){
answer= v2Data.get(i);
break;
}
}
}
return answer;
}