File tree Expand file tree Collapse file tree 4 files changed +146
-19
lines changed
solution/0600-0699/0671.Second Minimum Node In a Binary Tree Expand file tree Collapse file tree 4 files changed +146
-19
lines changed Original file line number Diff line number Diff line change 52
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
53
54
54
``` python
55
-
55
+ # Definition for a binary tree node.
56
+ # class TreeNode:
57
+ # def __init__(self, val=0, left=None, right=None):
58
+ # self.val = val
59
+ # self.left = left
60
+ # self.right = right
61
+ class Solution :
62
+ def findSecondMinimumValue (self , root : TreeNode) -> int :
63
+ def traverse (root , val ):
64
+ if root is None :
65
+ return
66
+ traverse(root.left, val)
67
+ traverse(root.right, val)
68
+ if root.val > val:
69
+ self .res = root.val if self .res == - 1 else min (self .res, root.val)
70
+
71
+ self .res = - 1
72
+ traverse(root, root.val)
73
+ return self .res
56
74
```
57
75
58
76
### ** Java**
59
77
60
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
79
62
80
``` java
63
-
81
+ /**
82
+ * Definition for a binary tree node.
83
+ * public class TreeNode {
84
+ * int val;
85
+ * TreeNode left;
86
+ * TreeNode right;
87
+ * TreeNode() {}
88
+ * TreeNode(int val) { this.val = val; }
89
+ * TreeNode(int val, TreeNode left, TreeNode right) {
90
+ * this.val = val;
91
+ * this.left = left;
92
+ * this.right = right;
93
+ * }
94
+ * }
95
+ */
96
+ class Solution {
97
+ private int res;
98
+ public int findSecondMinimumValue (TreeNode root ) {
99
+ res = - 1 ;
100
+ traverse(root, root. val);
101
+ return res;
102
+ }
103
+
104
+ private void traverse (TreeNode root , int min ) {
105
+ if (root == null ) {
106
+ return ;
107
+ }
108
+ traverse(root. left, min);
109
+ traverse(root. right, min);
110
+ if (root. val > min) {
111
+ res = res == - 1 ? root. val : Math . min(res, root. val);
112
+ }
113
+ }
114
+ }
64
115
```
65
116
66
117
### ** ...**
Original file line number Diff line number Diff line change 46
46
### ** Python3**
47
47
48
48
``` python
49
-
49
+ # Definition for a binary tree node.
50
+ # class TreeNode:
51
+ # def __init__(self, val=0, left=None, right=None):
52
+ # self.val = val
53
+ # self.left = left
54
+ # self.right = right
55
+ class Solution :
56
+ def findSecondMinimumValue (self , root : TreeNode) -> int :
57
+ def traverse (root , val ):
58
+ if root is None :
59
+ return
60
+ traverse(root.left, val)
61
+ traverse(root.right, val)
62
+ if root.val > val:
63
+ self .res = root.val if self .res == - 1 else min (self .res, root.val)
64
+
65
+ self .res = - 1
66
+ traverse(root, root.val)
67
+ return self .res
50
68
```
51
69
52
70
### ** Java**
53
71
54
72
``` java
55
-
73
+ /**
74
+ * Definition for a binary tree node.
75
+ * public class TreeNode {
76
+ * int val;
77
+ * TreeNode left;
78
+ * TreeNode right;
79
+ * TreeNode() {}
80
+ * TreeNode(int val) { this.val = val; }
81
+ * TreeNode(int val, TreeNode left, TreeNode right) {
82
+ * this.val = val;
83
+ * this.left = left;
84
+ * this.right = right;
85
+ * }
86
+ * }
87
+ */
88
+ class Solution {
89
+ private int res;
90
+ public int findSecondMinimumValue (TreeNode root ) {
91
+ res = - 1 ;
92
+ traverse(root, root. val);
93
+ return res;
94
+ }
95
+
96
+ private void traverse (TreeNode root , int min ) {
97
+ if (root == null ) {
98
+ return ;
99
+ }
100
+ traverse(root. left, min);
101
+ traverse(root. right, min);
102
+ if (root. val > min) {
103
+ res = res == - 1 ? root. val : Math . min(res, root. val);
104
+ }
105
+ }
106
+ }
56
107
```
57
108
58
109
### ** ...**
Original file line number Diff line number Diff line change 4
4
* int val;
5
5
* TreeNode left;
6
6
* TreeNode right;
7
- * TreeNode(int x) { val = x; }
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
8
14
* }
9
15
*/
10
16
class Solution {
17
+ private int res ;
11
18
public int findSecondMinimumValue (TreeNode root ) {
12
- if ( root == null || root . left == null ) return -1 ;
13
- int limit = Integer . MAX_VALUE ;
14
- Stack < TreeNode > stack = new Stack <>() ;
15
- stack . push ( root );
16
- while (! stack . isEmpty ()) {
17
- TreeNode node = stack . pop ();
18
- if (node . val != root . val ) {
19
- limit = Math . min ( limit , node . val - root . val ) ;
20
- }
21
- if ( node .left != null ) {
22
- stack . push ( node . left );
23
- stack . push ( node . right );
24
- }
19
+ res = -1 ;
20
+ traverse ( root , root . val ) ;
21
+ return res ;
22
+ }
23
+
24
+ private void traverse ( TreeNode root , int min ) {
25
+ if (root == null ) {
26
+ return ;
27
+ }
28
+ traverse ( root .left , min );
29
+ traverse ( root . right , min );
30
+ if ( root . val > min ) {
31
+ res = res == - 1 ? root . val : Math . min ( res , root . val );
25
32
}
26
- return limit == Integer .MAX_VALUE ? -1 : root .val + limit ;
27
33
}
28
34
}
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode:
3
+ # def __init__(self, val=0, left=None, right=None):
4
+ # self.val = val
5
+ # self.left = left
6
+ # self.right = right
7
+ class Solution :
8
+ def findSecondMinimumValue (self , root : TreeNode ) -> int :
9
+ def traverse (root , val ):
10
+ if root is None :
11
+ return
12
+ traverse (root .left , val )
13
+ traverse (root .right , val )
14
+ if root .val > val :
15
+ self .res = root .val if self .res == - 1 else min (self .res , root .val )
16
+
17
+ self .res = - 1
18
+ traverse (root , root .val )
19
+ return self .res
You can’t perform that action at this time.
0 commit comments