forked from algorithm001/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_671_134.java
More file actions
34 lines (33 loc) · 888 Bytes
/
Copy pathLeetCode_671_134.java
File metadata and controls
34 lines (33 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int first = Integer.MAX_VALUE;
private int second = Integer.MAX_VALUE;
private boolean flag = false;
public int findSecondMinimumValue(TreeNode root) {
if (root == null)
return -1;
travsing(root);
return flag?second:-1;
}
public void travsing(TreeNode node) {
if (node == null)
return ;
if (node.val < first)
first = node.val;
if (node.val <= second && node.val > first) {
second = node.val;
flag = true;
}
travsing(node.right);
travsing(node.left);
}
}