Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2213ae

Browse files
refactor 1676
1 parentdba2ab0 commita2213ae

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
publicclass_1676 {
99
publicstaticclassSolution1 {
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-
TreeNodelca;
16+
TreeNodelca =null;
1417

1518
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNode[]nodes) {
1619
Set<Integer>target =newHashSet<>();
@@ -37,4 +40,29 @@ private int dfs(TreeNode root, Set<Integer> target) {
3740
returnfoundCount;
3841
}
3942
}
43+
44+
publicstaticclassSolution2 {
45+
/**
46+
* Silly brute force way.
47+
*/
48+
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNode[]nodes) {
49+
TreeNodeans =nodes[0];
50+
for (inti =0;i <nodes.length;i++) {
51+
ans =lca(root,ans,nodes[i]);
52+
}
53+
returnans;
54+
}
55+
56+
privateTreeNodelca(TreeNoderoot,TreeNodep,TreeNodeq) {
57+
if (root ==null ||root ==p ||root ==q) {
58+
returnroot;
59+
}
60+
TreeNodeleft =lca(root.left,p,q);
61+
TreeNoderight =lca(root.right,p,q);
62+
if (left !=null &&right !=null) {
63+
returnroot;
64+
}
65+
returnleft !=null ?left :right;
66+
}
67+
}
4068
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importcom.fishercoder.common.classes.TreeNode;
44
importcom.fishercoder.common.utils.TreeUtils;
55
importcom.fishercoder.solutions._1676;
6+
importorg.junit.BeforeClass;
67
importorg.junit.Test;
78

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

1213
publicclass_1676Test {
1314
privatestatic_1676.Solution1solution1;
15+
privatestatic_1676.Solution2solution2;
16+
17+
@BeforeClass
18+
publicstaticvoidsetup() {
19+
solution1 =new_1676.Solution1();
20+
solution2 =new_1676.Solution2();
21+
}
1422

1523
@Test
1624
publicvoidtest1() {
17-
solution1 =new_1676.Solution1();
1825
TreeNoderoot =TreeUtils.constructBinaryTree(Arrays.asList(3,5,1,6,2,0,8,null,null,7,4));
26+
TreeUtils.printBinaryTree(root);
1927
TreeNodenode1 =TreeUtils.constructBinaryTree(Arrays.asList(4));
2028
TreeNodenode2 =TreeUtils.constructBinaryTree(Arrays.asList(7));
2129
TreeNode[]nodes =newTreeNode[]{node1,node2};
22-
TreeNodelca =TreeUtils.constructBinaryTree(Arrays.asList(2,7,4));
23-
assertEquals(lca,solution1.lowestCommonAncestor(root,nodes));
30+
TreeNodeexpected =TreeUtils.constructBinaryTree(Arrays.asList(2,7,4));
31+
assertEquals(expected,solution1.lowestCommonAncestor(root,nodes));
32+
}
33+
34+
@Test
35+
publicvoidtest2() {
36+
TreeNoderoot =TreeUtils.constructBinaryTree(Arrays.asList(3,5,1,6,2,0,8,null,null,7,4));
37+
TreeUtils.printBinaryTree(root);
38+
TreeNodenode1 =TreeUtils.constructBinaryTree(Arrays.asList(1,0,8));
39+
TreeNode[]nodes =newTreeNode[]{node1};
40+
TreeNodeexpected =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)

[8]ページ先頭

©2009-2025 Movatter.jp