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 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- 【转】odoo学习之:开发字段解析
odoo新API中,字段类型不变,继承改变 1.旧的API定义模型: from openerp.osv import osv,fields class oldmodel(osv.osv): #模型名称 ...
- 19、Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...
- XDS100V3连接Pandaboard ES OMAP4460开发板
1. 硬件连接如下 2. 使用CCS创建工程,不过好像没有ARM9的内核吧?为啥会出现? 3. 创建目标配置文件 4. 不过确实有ARM9的内核,两个A9内核,一个DSP C64X内核,两个M3的内核 ...
- PHP导出Excel,设置表格样式,填充颜色等较为复杂样式
// 注:只是在此做下记录,有兴趣的可以参考,不做实际教程文档 <?php //引入Li类对数据进行操作include_once('./Li.php');//引入Excel类库对对数据进行操作i ...
- loadrunner之做压力测试要做的准备
前提B/S架构 1.要有个备库和主库保存一致 到时候做压力测试的时候,要断开主库连接到备库.进行测试.以免主库出现垃圾数据.2.节点 判断单节点能承受多大的压力,如200万的用户账号,10万的在线用户 ...
- VIN码/车架号的详解,车架号识别,VIN码识别,OCR车架号识别能带来什么
各位车主在车检时不知道有没有注意到一件事,就是工作人员会打开车前盖在前围钢板上拓一张条码.下面来给大家介绍一下,这张条码就是VIN号,俗称钢印号,就像我们每个人都有自己的身份证号码一样,这也是汽车界的 ...
- request,logging,ConfigParser——接口框架
做一个将参数和用例分开放置,并且输出log的接口测试框架 我的框架如下所示 Log文件用来设置log输出文件,需要时可以在用例内调用输出,config用来填写一切需要的参数信息,jiekou_post ...
- 用Python实现多站点运维监控
在小型公司里如果产品线单一的话,比如就一个app, 一般1~2个运维就够用了.如果产品过于庞大,就需要多个运维人员. 但对于多产品线的公司来说,运维人员就要必须分多个人负责,因为超过200个站点让1个 ...
- [network]RIP协议
水平分割:一种避免路由环路的出现和加快路由汇聚的技术. 原理:路由器从某个接口接收到的更新信息不允许再从这个接口发送回去. 优点:1. 阻止路由环路产生:2. 减少路由器更新信息占用的链路带宽资源. ...
- CentOS7.3部署镜像仓库Harbor
参考文档: harbor介绍:https://github.com/vmware/harbor harbor安装&使用指导:https://github.com/vmware/harbor/b ...