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 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- removeAttribute与removeAttributeNode的区别
1.removeAttributeNode() 方法删除指定的属性,并以 Attr Node 对象返回被删除的属性. 例: <!DOCTYPE html><html><b ...
- pyqt5 菜单,工具栏,线程,matplotlib
import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QMainWindow, QMenuBar, QToolBar ...
- itop4412学习-上层应用多任务开发
1. 首先搭建虚拟机VMWARE12.0+UBUNTU16.04,不过报错了,说是要关闭计算机(非重启)-- 进入BIOS -- 设置BIOS的虚拟化(不打开,默认是工作在32位模式的,virtual ...
- luogu 2051 [AHOI2009]中国象棋
luogu 2051 [AHOI2009]中国象棋 真是一道令人愉♂悦丧心并框的好题... 首先"没有一个炮可以攻击到另一个炮"有个充分条件就是没有三个炮在同一行或同一列.证明:显 ...
- Awesome TensorFlow
Awesome TensorFlow A curated list of awesome TensorFlow experiments, libraries, and projects. Inspi ...
- css控制字体线使用:text-decoration
css控制字体下划线使用text-decoration : text-decoration:none 无装饰,通常对html下划线标签去掉下划线样式 text-decoration:underline ...
- KEIL5的安装
安装注意事项 1.最好不要安装在带有中文路径的文件夹. 2.试用版的Keil MDK只能编译32K以下的代码,代码大于32K只能使用正版或破解版才能编译通过. 安装MKD 这里选择MKD512A版本安 ...
- LJ语录
"保持安静,不要打扰我睡觉." ( 半分钟后) "哎呦喂~可以睡觉喽~" "考场上遇到这种题目,你们应该高兴." "因为考场上那群 ...
- python request 获取cookies value值的方法
import requests res = requests.get(url) cookies = requests.utils.dict_from_cookiejar(res.cookies) pr ...
- js 插件 issue
1 iscroll 5 和 lazyload 同时使用 转自 yinjie //lazyload var $scrollEle = $("#wrapper") $("i ...