Skip to content

Commit a2213ae

Browse files
refactor 1676
1 parent dba2ab0 commit a2213ae

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/main/java/com/fishercoder/solutions/_1676.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
public class _1676 {
99
public static class Solution1 {
1010
/**
11-
* Since there are conditions for this problem: all values in the tree and given nodes are unique, we could simply use a HashSet to track the number of nodes we've found so far during the traversal.
11+
* Since there are conditions for this problem: all values in the tree and given nodes are unique,
12+
* we could simply use a HashSet to track the number of nodes we've found so far during the traversal.
13+
* <p>
14+
* credit: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/discuss/958833/java-100
1215
*/
13-
TreeNode lca;
16+
TreeNode lca = null;
1417

1518
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
1619
Set<Integer> target = new HashSet<>();
@@ -37,4 +40,29 @@ private int dfs(TreeNode root, Set<Integer> target) {
3740
return foundCount;
3841
}
3942
}
43+
44+
public static class Solution2 {
45+
/**
46+
* Silly brute force way.
47+
*/
48+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
49+
TreeNode ans = nodes[0];
50+
for (int i = 0; i < nodes.length; i++) {
51+
ans = lca(root, ans, nodes[i]);
52+
}
53+
return ans;
54+
}
55+
56+
private TreeNode lca(TreeNode root, TreeNode p, TreeNode q) {
57+
if (root == null || root == p || root == q) {
58+
return root;
59+
}
60+
TreeNode left = lca(root.left, p, q);
61+
TreeNode right = lca(root.right, p, q);
62+
if (left != null && right != null) {
63+
return root;
64+
}
65+
return left != null ? left : right;
66+
}
67+
}
4068
}

src/test/java/com/fishercoder/_1676Test.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions._1676;
6+
import org.junit.BeforeClass;
67
import org.junit.Test;
78

89
import java.util.Arrays;
@@ -11,16 +12,34 @@
1112

1213
public class _1676Test {
1314
private static _1676.Solution1 solution1;
15+
private static _1676.Solution2 solution2;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1676.Solution1();
20+
solution2 = new _1676.Solution2();
21+
}
1422

1523
@Test
1624
public void test1() {
17-
solution1 = new _1676.Solution1();
1825
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4));
26+
TreeUtils.printBinaryTree(root);
1927
TreeNode node1 = TreeUtils.constructBinaryTree(Arrays.asList(4));
2028
TreeNode node2 = TreeUtils.constructBinaryTree(Arrays.asList(7));
2129
TreeNode[] nodes = new TreeNode[]{node1, node2};
22-
TreeNode lca = TreeUtils.constructBinaryTree(Arrays.asList(2, 7, 4));
23-
assertEquals(lca, solution1.lowestCommonAncestor(root, nodes));
30+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 7, 4));
31+
assertEquals(expected, solution1.lowestCommonAncestor(root, nodes));
32+
}
33+
34+
@Test
35+
public void test2() {
36+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4));
37+
TreeUtils.printBinaryTree(root);
38+
TreeNode node1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 8));
39+
TreeNode[] nodes = new TreeNode[]{node1};
40+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 8));
41+
assertEquals(expected, solution1.lowestCommonAncestor(root, nodes));
42+
assertEquals(expected, solution2.lowestCommonAncestor(root, nodes));
2443
}
2544

2645
}

0 commit comments

Comments
 (0)