-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCA_Naive.java
77 lines (65 loc) · 1.95 KB
/
LCA_Naive.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
public class LCA_Naive {
static class TreeNode {
int index;
TreeNode children[];
public TreeNode(int index) {
this.index = index;
children = new TreeNode[2];
}
}
/*
Sample Tree used in this code.
0
/ \
/ \
1 2
/ \ / \
/ \ / \
3 4 5 6
LCA of 1,2 = 0
LCA of 1,0 = 0
LCA of 3,4 = 1
LCA of 4,5 = 0
LCA of 3,3 = 3
*/
public static TreeNode lca (TreeNode at, int a, int b) {
if (at == null)
return null;
if (at.index == a || at.index == b)
return at;
TreeNode left = lca(at.children[0], a, b);
TreeNode right = lca(at.children[1], a, b);
if (left != null && right != null)
return at;
if (left != null)
return left;
else
return right;
}
public static void lcaHelper(int a, int b, int n, TreeNode root) {
if (isValid(a, b, n)){
TreeNode lca = lca(root, a, b);
System.out.println("LCA is " + lca.index + "\n");
} else {
System.out.println("Not valid input\n");
}
}
public static boolean isValid (int a, int b, int n) {
return 0<= a && a < n && 0<= b && b < n;
}
public static void main(String[] args) {
// Construct Sample Tree
int n = 7; // The Number of node
TreeNode root = new TreeNode(0);
root.children[0] = new TreeNode(1);
root.children[1] = new TreeNode(2);
root.children[0].children[0] = new TreeNode(3);
root.children[0].children[1] = new TreeNode(4);
root.children[1].children[0] = new TreeNode(5);
root.children[1].children[1] = new TreeNode(6);
// Case 1: Query about LCA of 3, 5
lcaHelper(3, 5, n, root);
// Case 2: Query about LCA of 8, 5
lcaHelper(8, 5, n, root);
}
}