-
Notifications
You must be signed in to change notification settings - Fork 0
/
671. 二叉树中第二小的节点.java
66 lines (58 loc) · 1.64 KB
/
671. 二叉树中第二小的节点.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* O(n)
*/
class Solution {
int res;
int rootVal;
public int findSecondMinimumValue(TreeNode root) {
res = -1;
rootVal = root.val;
dfs(root);
return res;
}
private void dfs(TreeNode root){
if(root == null){
return;
}
if(res != -1 && root.val > res){ // 提前终止,如果res不为默认值,并且当前结点已经比res大了,则当前结点的子孙节点已经没有遍历的意义了
return;
}
if(res == -1 && root.val != rootVal){ // 如果是res是初始值,并且有一个值和根结点不相等,则更新res值
res = root.val;
}else if(res != -1 && root.val > rootVal && root.val < res){ // 如果当前结点的值在rootVal和res之间,则更新
res = root.val;
}
dfs(root.left);
dfs(root.right);
}
}
/**
* O(n * log(n))
*/
class Solution {
public int findSecondMinimumValue(TreeNode root) {
PriorityQueue<Integer> pq = new PriorityQueue<>(); // 堆
dfs(root, pq);
if(pq.size() == 0){
return -1;
}
int firMin = pq.poll(); // 取最小的
int secMin = -1; // 取第二小的
while(!pq.isEmpty()){
int temp = pq.poll();
if(temp != firMin){
secMin = temp;
break;
}
}
return secMin;
}
private void dfs(TreeNode root, PriorityQueue<Integer> pq){
if(root == null){
return;
}
pq.offer(root.val);
dfs(root.left, pq);
dfs(root.right, pq);
}
}