-
Notifications
You must be signed in to change notification settings - Fork 11
/
LowestCommonAncestor.java
75 lines (60 loc) Β· 2.2 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
package trees;
import java.util.ArrayList;
import java.util.Scanner;
public class LowestCommonAncestor {
public static boolean flag = false;
public static void main(String[] args) {
CustomTree<Integer> root = new CustomTree<>();
root.input();
root.print();
Scanner s = new Scanner(System.in);
int data1 = s.nextInt();
int data2 = s.nextInt();
CustomTree<Integer> node = lowestCommonAncestor(root, data1, data2);
System.out.println("\n" + node.data);
}
private static CustomTree<Integer> lowestCommonAncestor(CustomTree<Integer> root, int data1, int data2){
ArrayList<CustomTree<Integer>> list1 = ancestorOf(root, data1); flag = false;
ArrayList<CustomTree<Integer>> list2 = ancestorOf(root, data2); flag = false;
print(list1);
print(list2);
int i;
for(i=0 ; i<Math.min(list1.size(), list2.size()) ; i++){
if(list1.get(list1.size() - i - 1).data != list2.get(list2.size() - i - 1).data){
break;
}
}
return list1.get(list1.size()-i);
}
private static ArrayList<CustomTree<Integer>> ancestorOf(CustomTree<Integer> root, int data){
if(root == null){
return new ArrayList<CustomTree<Integer>>();
}
if((int)root.data == data){
ArrayList<CustomTree<Integer>> smallList = new ArrayList<>();
smallList.add(root);
flag = true;
return smallList;
}
if(!flag) {
ArrayList<CustomTree<Integer>> smallList1 = ancestorOf(root.left, data);
if (flag) {
smallList1.add(root);
return smallList1;
} else {
ArrayList<CustomTree<Integer>> smallList2 = ancestorOf(root.right, data);
if(flag){
smallList2.add(root);
return smallList2;
}
}
}
return new ArrayList<>();
}
private static void print(ArrayList<CustomTree<Integer>> list){
System.out.println("");
for(int i=0 ; i<list.size() ; i++){
System.out.print(list.get(i).data + " ");
}
}
}