Skip to content

Commit 8a28586

Browse files
committed
feat: add the solution of Second Minimum Node In a Binary Tree(671) with Java.
1 parent 45d35a2 commit 8a28586

File tree

3 files changed

+105
-25
lines changed

3 files changed

+105
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
| [661][661-question] | [Image Smoother][661-tips] | [][661-java] | [][661-js] | |
7070
| [665][665-question] | [Non-decreasing Array][665-tips] | [][665-java] | [][665-js] | |
7171
| [669][669-question] | [Trim a Binary Search Tree][669-tips] | [][669-java] | [][669-js] | |
72-
| [671][671-question] | [Second Minimum Node In a Binary Tree][671-tips] | | [][671-js] | |
72+
| [671][671-question] | [Second Minimum Node In a Binary Tree][671-tips] | [][671-java] | [][671-js] | |
7373
| [674][674-question] | [Longest Continuous Increasing Subsequence][674-tips] | | [][674-js] | |
7474
| [680][680-question] | [Valid Palindrome II][680-tips] | | [][680-js] | |
7575
| [682][682-question] | [Baseball Game][682-tips] | | [][682-js] | |
@@ -462,6 +462,7 @@
462462
[661-java]: ./src/_661/Solution.java
463463
[665-java]: ./src/_665/Solution.java
464464
[669-java]: ./src/_669/Solution.java
465+
[671-java]: ./src/_671/Solution.java
465466
[728-java]: ./src/_728/Solution.java
466467
[771-java]: ./src/_771/Solution.java
467468
[804-java]: ./src/_804/Solution.java

src/_671/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package _671;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* public class TreeNode {
8+
* int val;
9+
* TreeNode left;
10+
* TreeNode right;
11+
* TreeNode(int x) { val = x; }
12+
* }
13+
*/
14+
class Solution {
15+
public int findSecondMinimumValue(TreeNode root) {
16+
Stack<TreeNode> stack = new Stack<>();
17+
int min = root.val;
18+
int secMin = -1;
19+
stack.push(root);
20+
21+
while (!stack.empty()) {
22+
TreeNode node = stack.pop();
23+
24+
if (node.val > min) {
25+
if (node.val < secMin || secMin == -1) {
26+
secMin = node.val;
27+
}
28+
continue;
29+
}
30+
31+
if (node.left != null) {
32+
stack.push(node.right);
33+
stack.push(node.left);
34+
}
35+
}
36+
37+
return secMin;
38+
}
39+
40+
class TreeNode {
41+
int val;
42+
TreeNode left;
43+
TreeNode right;
44+
45+
TreeNode(int x) {
46+
val = x;
47+
}
48+
}
49+
}

tips/671/README.md

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,76 @@
1-
[xxxx][title]
1+
[Second Minimum Node In a Binary Tree][title]
22

33
## Description
4-
// 抄题目
4+
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly `two` or `zero` sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.
55

6+
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.
67

7-
**Example:**
8+
If no such second minimum value exists, output -1 instead.
9+
10+
**Example 1:**
811

912
```
10-
// 抄Example
13+
Input:
14+
2
15+
/ \
16+
2 5
17+
/ \
18+
5 7
19+
20+
Output: 5
21+
Explanation: The smallest value is 2, the second smallest value is 5.
1122
```
1223

13-
**Note:**
14-
// Note
15-
16-
**Tags:** // tags
17-
18-
19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
24+
**Example 2:**
2325

2426
```
25-
```javascript
27+
Input:
28+
2
29+
/ \
30+
2 2
2631
32+
Output: -1
33+
Explanation: The smallest value is 2, but there isn't any second smallest value.
2734
```
2835

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
36+
**Tags:** [Tree](https://leetcode.com/tag/tree/)
3237

33-
```
38+
## 思路
39+
40+
题目给出一个特殊的二叉树:任意节点,要么没有子节点,要么有两个子节点,且子节点的值会大于等于他们的根节点。要求找出第二小的节点值。根据此二叉树的特性,根节点的值为最小值。先对树进行先序遍历,当发现比根节点值(最小值)大的节点后,后面的子树就可以进行剪枝了,因为后面的子节点都大于或等于此节点,不会是第二小的节点(或者与第二小的节点值相等)。
3441

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
42+
**Java:**
3843

44+
```java
45+
public int findSecondMinimumValue(TreeNode root) {
46+
Stack<TreeNode> stack = new Stack<>();
47+
int min = root.val;
48+
int secMin = -1;
49+
stack.push(root);
50+
51+
while (!stack.empty()) {
52+
TreeNode node = stack.pop();
53+
54+
if (node.val > min) {
55+
if (node.val < secMin || secMin == -1) {
56+
secMin = node.val;
57+
}
58+
continue;
59+
}
60+
61+
if (node.left != null) {
62+
stack.push(node.right);
63+
stack.push(node.left);
64+
}
65+
}
66+
67+
return secMin;
68+
}
3969
```
4070

4171
## 结语
42-
72+
4373
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4474

45-
[title]: https://leetcode.com/problems/xxxx
75+
[title]: https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/
4676
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)