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

Commit00c8d38

Browse files
add 1676
1 parent6b397fe commit00c8d38

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ _If you like this project, please leave me a star._ ★
115115
|1680|[Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1680.java)||Medium|Math|
116116
|1679|[Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1679.java)||Medium|HashTable|
117117
|1678|[Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1678.java)||Easy|String|
118+
|1676|[Lowest Common Ancestor of a Binary Tree IV](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1676.java)||Medium|Tree, DFS, Binary Tree|
118119
|1675|[Minimize Deviation in Array](https://leetcode.com/problems/minimize-deviation-in-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1675.java)||Hard|Heap, Ordered Map|
119120
|1673|[Find the Most Competitive Subsequence](https://leetcode.com/problems/find-the-most-competitive-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1673.java)|[:tv:](https://youtu.be/GBJFxSD3B_s)|Medium|Stack, Greedy|
120121
|1672|[Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1672.java)||Easy|Array|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
5+
importjava.util.HashSet;
6+
importjava.util.Set;
7+
8+
publicclass_1676 {
9+
publicstaticclassSolution1 {
10+
/**
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.
12+
*/
13+
TreeNodelca;
14+
15+
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNode[]nodes) {
16+
Set<Integer>target =newHashSet<>();
17+
for (TreeNodenode :nodes) {
18+
target.add(node.val);
19+
}
20+
dfs(root,target);
21+
returnlca;
22+
}
23+
24+
privateintdfs(TreeNoderoot,Set<Integer>target) {
25+
if (root ==null) {
26+
return0;
27+
}
28+
intleftCount =dfs(root.left,target);
29+
intrightCount =dfs(root.right,target);
30+
intfoundCount =leftCount +rightCount;
31+
if (target.contains(root.val)) {
32+
foundCount++;
33+
}
34+
if (foundCount ==target.size() &&lca ==null) {
35+
lca =root;
36+
}
37+
returnfoundCount;
38+
}
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.TreeUtils;
5+
importcom.fishercoder.solutions._1676;
6+
importorg.junit.Test;
7+
8+
importjava.util.Arrays;
9+
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_1676Test {
13+
privatestatic_1676.Solution1solution1;
14+
15+
@Test
16+
publicvoidtest1() {
17+
solution1 =new_1676.Solution1();
18+
TreeNoderoot =TreeUtils.constructBinaryTree(Arrays.asList(3,5,1,6,2,0,8,null,null,7,4));
19+
TreeNodenode1 =TreeUtils.constructBinaryTree(Arrays.asList(4));
20+
TreeNodenode2 =TreeUtils.constructBinaryTree(Arrays.asList(7));
21+
TreeNode[]nodes =newTreeNode[]{node1,node2};
22+
TreeNodelca =TreeUtils.constructBinaryTree(Arrays.asList(2,7,4));
23+
assertEquals(lca,solution1.lowestCommonAncestor(root,nodes));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp