-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLCA_BST.java
37 lines (33 loc) · 912 Bytes
/
LCA_BST.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
//User function Template for Java
class BST
{
//Function to find the lowest common ancestor in a BST.
Node LCA(Node root, int n1, int n2)
{
List<Node> lst1 = findPath(root, n1);
List<Node> lst2 = findPath(root, n2);
Set<Node> set = new HashSet<>(lst1);
for(int i = lst2.size() - 1; i >= 0; i--){
if(set.contains(lst2.get(i))){
return lst2.get(i);
}
}
return null;
}
List<Node> findPath(Node root, int target){
List<Node> path = new ArrayList<>();
while(root != null){
path.add(root);
if(root.data == target){
break;
}
if(root.data < target){
root = root.right;
}
else{
root = root.left;
}
}
return path;
}
}