-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-671.java
More file actions
31 lines (31 loc) · 891 Bytes
/
lc-671.java
File metadata and controls
31 lines (31 loc) · 891 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
//private int min = Integer.MAX_VALUE, smin = Integer.MAX_VALUE;
public int findSecondMinimumValue(TreeNode root) {
//层次遍历
Integer min = Integer.MAX_VALUE, smin = null;
Queue<TreeNode> q = new LinkedList();
q.offer(root);
while(!q.isEmpty()) {
TreeNode n = q.poll();
if(n.val <= min) {
min = n.val;
}else if(smin != null) {
if(n.val < smin) smin = n.val;
}else {
smin = n.val;
}
if(n.left != null)q.offer(n.left);
if(n.right != null)q.offer(n.right);
}
return (smin == null)?-1:smin;
}
}