Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2f1856a

Browse files
add 671
1 parent b11d11a commit 2f1856a

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+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.Iterator;
6+
import java.util.Set;
7+
import java.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+
public class _671 {
42+
public static class Solution1 {
43+
public int findSecondMinimumValue(TreeNode root) {
44+
if (root == null) {
45+
return -1;
46+
}
47+
Set<Integer> set = new TreeSet<>();
48+
dfs(root, set);
49+
Iterator<Integer> iterator = set.iterator();
50+
int count = 0;
51+
while (iterator.hasNext()) {
52+
count++;
53+
int result = iterator.next();
54+
if (count == 2) {
55+
return result;
56+
}
57+
}
58+
return -1;
59+
}
60+
61+
private void dfs(TreeNode root, 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+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._671;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _671Test {
14+
private static _671.Solution1 solution1;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _671.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 5, null, null, 5, 7));
25+
assertEquals(5, solution1.findSecondMinimumValue(root));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2));
31+
assertEquals(-1, solution1.findSecondMinimumValue(root));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)