LeetCode 671. Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.
Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.
If no such second minimum value exists, output -1 instead.
Example 1:
Input:
2
/ \
2 5
/ \
5 7 Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.
Example 2:
Input:
2
/ \
2 2 Output: -1
Explanation: The smallest value is 2, but there isn't any second smallest value. 题意:给定一颗二叉树,返回二叉树中第二最小的结点值,如果不存在第二最小值,则返回-1。对于给定二叉树的规定:
1. 二叉树非空,且二叉树中的结点值非负;
2. 二叉树中每个结点要么有两个子结点,要么没有子结点;
如果某父结点有两个子结点,则父结点是两个子结点中的较小值,其中两个子结点值可以相等。
思路:根结点一定是最小值。采用遍历的方法逐个判断是否为第二小元素,实现如下:
public int findSecondMinimumValue(TreeNode root) {
int sMin = root.val, min = root.val;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while(!q.isEmpty()){
TreeNode t = q.poll();
if((sMin == min || t.val < sMin) && t.val > min)//sMin == min来获取第一个比root.val大的元素
sMin = t.val;
if(t.left != null)
q.offer(t.left);
if(t.right != null)
q.offer(t.right);
}
return sMin == min ? -1 : sMin;
}
LeetCode提供的算法1:
先使用深度优先搜索的方法遍历二叉树,并利用set不包含重复元素的特性,将树中的结点存入set对象;然后再找第二小的元素。
public void dfs(TreeNode root, Set<Integer> uniques){
if(root != null){
uniques.add(root.val);
dfs(root.left, uniques);
dfs(root.right, uniques);
}
}
public int findSecondMinimumValue(TreeNode root){
Set<Integer> uniques = new HashSet<>();
dfs(root, uniques);
int min = root.val;
long second = Long.MAX_VALUE;
for(int val : uniques){
if(val > min && val < second)
second = val;
}
return (int) (second == Long.MAX_VALUE ? -1 : second);
}
算法2:对算法1进行改进,因为只需要找第二小的值,因此不需要存储树中所有不重复出现的元素。当遍历某个结点node时,如果node.val > min,则说明该结点node的子树的所有值都大于等于node.val,因此结点node的子树中就不用再判断了。
int min;
long ans = Long.MAX_VALUE;
public void dfs(TreeNode root){
if(root != null){
if(root.val > min && root.val < ans)
ans = root.val;
else if(root.val == min){
dfs(root.left);
dfs(root.right);
}
}
}
public int findSecondMinimumValue(TreeNode root){
min = root.val;
dfs(root);
return ans < Long.MAX_VALUE ? (int) ans : -1;
}
LeetCode 671. Second Minimum Node In a Binary Tree的更多相关文章
- LeetCode 671. Second Minimum Node In a Binary Tree二叉树中第二小的节点 (C++)
题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where eac ...
- 【Leetcode_easy】671. Second Minimum Node In a Binary Tree
problem 671. Second Minimum Node In a Binary Tree 参考 1. Leetcode_easy_671. Second Minimum Node In a ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- [LeetCode&Python] Problem 671. Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...
- Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
- 【easy】671. Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...
- 671. Second Minimum Node In a Binary Tree 非递减二叉树中第二小的元素
[抄题]: Given a non-empty special binary tree consisting of nodes with the non-negative value, where e ...
- 671. Second Minimum Node In a Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- 【洛谷P2245】星际导航
题面 题解 \(kruskal\)重构树板子题??(大雾 因为重构树上两点之间的\(LCA\)的权值就是原图上最小生成树上的瓶颈. 所以建个重构树,跑\(LCA\)即可. 代码 #include< ...
- 工作中遇到的令人头疼的bug
工作中我们会遇到形形色色的bug,但是很多bug都可以调试很明显的看出来,这种bug解决起来我们不会那么头疼但是有些却让人头疼而捉急,特别是本地运行一切正常,上传服务器就会出现bug.现在我总结几个我 ...
- IDE看代码,挺好
初学编程的时候总是收到各种警告:“刚学习编程千万不要用IDE,否则会有xxxxxx的后果”.现在工作后发现使用IDE可以方便编写和查看代码,对于较大的项目来说有很多代码,代码之间的关系也比较复杂,ID ...
- zepto 添加 animate组件
今天发现JQuery可以用 animate方法回到顶部,心想着zepto应该也可以 $('html,body').animate({ scrollTop: 0 }, 1000); 于是便用了一下,发现 ...
- LeetCode 845——数组中的最长山脉
1. 题目 2. 解答 2.1 方法一 left 数组表示当前元素左边比当前元素小的元素个数,right 数组数组表示当前元素右边比当前元素小的元素个数.在山脉的中间 B[i] 处,其左边和右边肯定都 ...
- 【RL系列】马尔可夫决策过程——Jack‘s Car Rental
本篇请结合课本Reinforcement Learning: An Introduction学习 Jack's Car Rental是一个经典的应用马尔可夫决策过程的问题,翻译过来,我们就直接叫它“租 ...
- PKI(Public Key Infrastucture)介绍
PKI(Public Key Infrastucture)介绍 根据Wikipedia PKI词条整理. PKI(Public Key Infrastucture)是一系列的规则.策略以及过程,可以用 ...
- C++ STL 优先队列 priority_queue 详解(转)
转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...
- Binary Tree(生成二叉树)
Description Background Binary trees are a common data structure in computer science. In this problem ...
- c# 画image
这是一个例子,从数据库中读取然后赋伪彩,生成bitmap,给到imagebox控件(其image属性为平铺). https://pan.baidu.com/s/1hf_fGFHjGoDK_gywuhg ...