- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1644.java
107 lines (97 loc) · 3.62 KB
/
_1644.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
packagecom.fishercoder.solutions.secondthousand;
importcom.fishercoder.common.classes.TreeNode;
publicclass_1644 {
publicstaticclassSolution1 {
/*
* This is my not so elegant but original solution to get it accepted.
*/
boolean[] exists = newboolean[2];
publicTreeNodelowestCommonAncestor(TreeNoderoot, TreeNodep, TreeNodeq) {
exists(p, root, 0);
exists(q, root, 1);
if (!exists[0] || !exists[1]) {
returnnull;
}
returndfs(root, p, q);
}
privatevoidexists(TreeNodetarget, TreeNoderoot, intindex) {
if (root == null) {
return;
}
if (target == root) {
exists[index] = true;
return;
}
if (!exists[index]) {
exists(target, root.left, index);
}
if (!exists[index]) {
exists(target, root.right, index);
}
}
privateTreeNodedfs(TreeNoderoot, TreeNodep, TreeNodeq) {
if (root == null || p == root || q == root) {
returnroot;
}
TreeNodeleft = lowestCommonAncestor(root.left, p, q);
TreeNoderight = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
returnroot;
}
returnleft != null ? left : right;
}
}
publicstaticclassSolution2 {
/*
* This still checks nodes existence.
*/
intfound = 0;
publicTreeNodelowestCommonAncestor(TreeNoderoot, TreeNodep, TreeNodeq) {
TreeNodelca = lca(root, p, q);
returnfound == 2 ? lca : null;
}
privateTreeNodelca(TreeNoderoot, TreeNodep, TreeNodeq) {
if (root == null) {
returnnull;
}
TreeNodeleft = lca(root.left, p, q);
TreeNoderight = lca(root.right, p, q);
if (root == p || root == q) {
found++;
returnroot;
}
return (left != null && right != null) ? root : left != null ? left : right;
}
}
publicstaticclassSolution3 {
/*
* Credit: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/solutions/944963/beat-96-recursion-without-count-easy-understanding/
*/
publicTreeNodelowestCommonAncestor(TreeNoderoot, TreeNodep, TreeNodeq) {
if (root == null || p == null || q == null) {
returnnull;
}
TreeNoderesult = findLCA(root, p, q);
if (result == p) {
// if p equals result, we'll check the existence of q in the subtree of p
returnfindLCA(p, q, q) != null ? result : null;
} elseif (result == q) {
// if q equals result, we'll check the existence of p in the subtree of q
returnfindLCA(q, p, p) != null ? result : null;
}
// otherwise, it's this case: (p != result && q != result) || result == null
returnresult;
}
privateTreeNodefindLCA(TreeNoderoot, TreeNodep, TreeNodeq) {
if (root == null || p == root || q == root) {
returnroot;
}
TreeNodeleft = findLCA(root.left, p, q);
TreeNoderight = findLCA(root.right, p, q);
if (left != null && right != null) {
returnroot;
}
returnleft != null ? left : right;
}
}
}