forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1644.java
74 lines (66 loc) · 2.3 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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _1644 {
public static class Solution1 {
/**
* This is my not so elegant but original solution to get it accepted.
*/
boolean[] exists = new boolean[2];
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
exists(p, root, 0);
exists(q, root, 1);
if (!exists[0] || !exists[1]) {
return null;
}
return dfs(root, p, q);
}
private void exists(TreeNode target, TreeNode root, int index) {
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);
}
}
private TreeNode dfs(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || p == root || q == root) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
return root;
}
return left != null ? left : right;
}
}
public static class Solution2 {
/**
* This satisfies the follow-up question: Can you find the LCA traversing the tree, without checking nodes existence?
*/
int found = 0;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode lca = lca(root, p, q);
return found == 2 ? lca : null;
}
private TreeNode lca(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
TreeNode left = lca(root.left, p, q);
TreeNode right = lca(root.right, p, q);
if (root == p || root == q) {
found++;
return root;
}
return (left != null && right != null) ? root : left != null ? left : right;
}
}
}