leetcode-671-二叉树中第二小的节点
[博客链接]
[题目描述]
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一
个。
更正式地说,root.val = min(root.left.val, root.right.val) 总成立。
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。
示例 1:
输入:root = [2,2,5,null,null,5,7]
输出:5
解释:最小的值是 2 ,第二小的值是 5 。
示例 2:
输入:root = [2,2,2]
输出:-1
解释:最小的值是 2, 但是不存在第二小的值。
提示:
树中节点数目在范围 [1, 25] 内
1 <= Node.val <= 231 - 1
对于树中每个节点 root.val == min(root.left.val, root.right.val)
Related Topics 树 深度优先搜索 二叉树
👍 159 👎 0
[题目链接]
[github地址]
[思路介绍]
思路一:DFS
- 从题目分析来看只需要判断第一个根结点的子节点满足不满足要求即可
- 想的有些简单了,较大值不一定满足要求,所以还是dfs遍历+小根堆看吧
- 其实我在想set是不是也可以
- 不满足则递归遍历,找到满足的节点
- 找不到则返回-1
public int findSecondMinimumValue(TreeNode root) {
//corner case
PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(new Comparator<Integer>() {
//小根堆
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2) return 1;
return o1.equals(o2) ? 0 : -1;
}
});
dfs(root, priorityQueue);
if (priorityQueue.size() >= 2) {
int min = priorityQueue.poll();
while (priorityQueue.peek()!=null){
if (priorityQueue.peek()>min){
return priorityQueue.poll();
}else{
priorityQueue.poll();
}
}
}
return -1;
}
private void dfs(TreeNode root, PriorityQueue<Integer> queue) {
if (root == null) {
return;
}
queue.add(root.val);
dfs(root.left, queue);
dfs(root.right, queue);
}
时间复杂度O(n)
set写法
private void dfs(TreeNode root, Set<Integer> queue) {
if (root == null) {
return;
}
queue.add(root.val);
dfs(root.left, queue);
dfs(root.right, queue);
}
public int findSecondMinimumValue(TreeNode root) {
//corner case
Set<Integer> set = new TreeSet<>(new Comparator<Integer>() {
//小根堆
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2) return 1;
return o1.equals(o2) ? 0 : -1;
}
});
dfs(root, set);
if (set.size() >= 2) {
int i = 0;
for (int num:set
) {
if (i == 1){
return num;
}else{
i++;
}
}
}
return -1;
}
时间复杂度O(n)
思路二:dfs+剪枝
- 利用本题二叉树性质剪枝
- 较大元素的子节点是不需要遍历的,所有我们留一个作为备份即可
- 也就是dfs可以不用两边都遍历
public int findSecondMinimumValue(TreeNode root) {
//corner case
if (root.left == null){
return -1;
}
Set<Integer> set = new TreeSet<>(new Comparator<Integer>() {
//小根堆
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2) return 1;
return o1.equals(o2) ? 0 : -1;
}
});
set.add(root.left.val);
set.add(root.right.val);
if (root.left.val == root.val) {
dfs3(root.left, set);
}
if (root.right.val == root.val) {
dfs3(root.right, set);
}
if (set.size() >= 2) {
int i = 0;
for (int num : set
) {
if (i == 1) {
return num;
} else {
i++;
}
}
}
return -1;
}
private void dfs3(TreeNode root, Set<Integer> set) {
if (root == null) {
return;
}
if (root.left == null) {
return;
}
set.add(root.left.val);
set.add(root.right.val);
if (root.left.val == root.val) {
dfs3(root.left, set);
}
if (root.right.val == root.val) {
dfs3(root.right, set);
}
}
时间复杂度O(n)
思路三:剪枝的进一步优化
-
其实之前解题时里想到过这种思路
-
但是还是没有抓住重点就没继续往下思考
-
这种思路的依靠的条件有两个
- 节点都是正数,保证了ans只有遇到正数时才会更新
- 节点最小值向上传递,也就是保证了根结点的最小值
-
这样的逻辑也就保证了ans只会在遇到了比最小值大的节点的时才会更新
-
同时ans更新后不必再向下遍历
-
剪枝的进一步优化-剪掉所有子节点
int ans = -1;
public int findSecondMinimumValue(TreeNode root) {
dfs(root, root.val);
return ans;
}
void dfs(TreeNode root, int cur) {
if (root == null) return ;
if (root.val != cur) {
if (ans == -1) ans = root.val;
else ans = Math.min(ans, root.val);
return ;
}
dfs(root.left, cur);
dfs(root.right, cur);
}
时间复杂度O(n)