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

Commit2f1856a

Browse files
add 671
1 parentb11d11a commit2f1856a

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|671|[Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_671.java) | O(n) | O(n) | Easy | Tree, DFS
2526
|668|[Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_668.java) | O(logm*n) | O(1) | Hard | Binary Search
2627
|667|[Beautiful Arrangement II](https://leetcode.com/problems/beautiful-arrangement-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_667.java) | O(n) | O(1) | Medium | Array
2728
|666|[Path Sum IV](https://leetcode.com/problems/path-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_666.java) | O(1) | O(1) | Medium | Tree, DFS
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
5+
importjava.util.Iterator;
6+
importjava.util.Set;
7+
importjava.util.TreeSet;
8+
9+
/**
10+
* 671. Second Minimum Node In a Binary Tree
11+
*
12+
* Given a non-empty special binary tree consisting of nodes with the non-negative value,
13+
* where each node in this tree has exactly two or zero sub-node.
14+
* If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.
15+
* Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.
16+
* If no such second minimum value exists, output -1 instead.
17+
18+
Example 1:
19+
20+
Input:
21+
2
22+
/ \
23+
2 5
24+
/ \
25+
5 7
26+
27+
Output: 5
28+
Explanation: The smallest value is 2, the second smallest value is 5.
29+
30+
Example 2:
31+
32+
Input:
33+
2
34+
/ \
35+
2 2
36+
37+
Output: -1
38+
Explanation: The smallest value is 2, but there isn't any second smallest value.
39+
40+
*/
41+
publicclass_671 {
42+
publicstaticclassSolution1 {
43+
publicintfindSecondMinimumValue(TreeNoderoot) {
44+
if (root ==null) {
45+
return -1;
46+
}
47+
Set<Integer>set =newTreeSet<>();
48+
dfs(root,set);
49+
Iterator<Integer>iterator =set.iterator();
50+
intcount =0;
51+
while (iterator.hasNext()) {
52+
count++;
53+
intresult =iterator.next();
54+
if (count ==2) {
55+
returnresult;
56+
}
57+
}
58+
return -1;
59+
}
60+
61+
privatevoiddfs(TreeNoderoot,Set<Integer>set) {
62+
if (root ==null) {
63+
return;
64+
}
65+
set.add(root.val);
66+
dfs(root.left,set);
67+
dfs(root.right,set);
68+
return;
69+
}
70+
}
71+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.TreeUtils;
5+
importcom.fishercoder.solutions._671;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importjava.util.Arrays;
10+
11+
importstaticorg.junit.Assert.assertEquals;
12+
13+
publicclass_671Test {
14+
privatestatic_671.Solution1solution1;
15+
privatestaticTreeNoderoot;
16+
17+
@BeforeClass
18+
publicstaticvoidsetup() {
19+
solution1 =new_671.Solution1();
20+
}
21+
22+
@Test
23+
publicvoidtest1() {
24+
root =TreeUtils.constructBinaryTree(Arrays.asList(2,2,5,null,null,5,7));
25+
assertEquals(5,solution1.findSecondMinimumValue(root));
26+
}
27+
28+
@Test
29+
publicvoidtest2() {
30+
root =TreeUtils.constructBinaryTree(Arrays.asList(2,2,2));
31+
assertEquals(-1,solution1.findSecondMinimumValue(root));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp