File tree Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Original file line number Diff line number Diff line change 13
13
* }
14
14
* }
15
15
*/
16
- class Solution {
17
- Integer minimum ;
18
- long secondMinimum ;
16
+ class Solution {
19
17
public int findSecondMinimumValue (TreeNode root ) {
20
- minimum = root .val ;
21
- secondMinimum = Long .MAX_VALUE ;
22
- helper (root );
23
- return secondMinimum == Long .MAX_VALUE ? -1 : (int ) secondMinimum ;
18
+ long [] topTwo = {root .val , Long .MAX_VALUE };
19
+ helper (root , topTwo );
20
+ return topTwo [1 ] == Long .MAX_VALUE ? -1 : (int ) topTwo [1 ];
24
21
}
25
22
26
- private void helper (TreeNode root ) {
23
+ private void helper (TreeNode root , long [] topTwo ) {
27
24
if (root == null ) {
28
25
return ;
29
26
}
30
- if (minimum < root .val && root .val < secondMinimum ) {
31
- secondMinimum = root .val ;
32
- }
33
- else if (root .val == minimum ) {
34
- helper (root .left );
35
- helper (root .right );
27
+ if (topTwo [0 ] < root .val && root .val < topTwo [1 ]) {
28
+ topTwo [1 ] = root .val ;
29
+ } else {
30
+ helper (root .left , topTwo );
31
+ helper (root .right , topTwo );
36
32
}
37
33
}
38
34
}
You can’t perform that action at this time.
0 commit comments