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 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.
分析:
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。
首先想到可以遍历树,将节点值存进一个set中,最后返回set中第二小的值,如果只有一个元素就返回-1。
不过题中的二叉树是特殊的,每个节点的子节点数量只能为2或0,且这个节点的值不大于它的子节点的值。基于这个特点,我们可以不使用set。遍历每个节点的时候判断当前节点是否大于等于第二小的值,因为题中的二叉树的特点,节点的子节点是大于等于当前节点的,我没就没有必要在去访问后面的节点了。
程序:
//use set
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
findmin(root);
set<int>::iterator it = s.begin();
if (s.size() > )
return *(++it);
else
return -; }
void findmin(TreeNode* root){
if(root == nullptr) return;
s.insert(root->val);
findmin(root->left);
findmin(root->right);
}
private:
set<int> s;
};
//no set
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
finds(root);
if(smin != LONG_MAX) return (int)smin;
else return -;
}
void finds(TreeNode* root){
if(root == nullptr) return;
if(smin <= root->val) return;
if(root->val < fmin){
fmin = root->val;
}
else if(root->val < smin && root->val > fmin){
smin = root->val;
}
finds(root->left);
finds(root->right);
}
private:
long fmin = LONG_MAX;
long smin = LONG_MAX;
};
LeetCode 671. Second Minimum Node In a Binary Tree二叉树中第二小的节点 (C++)的更多相关文章
- [LeetCode] 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 ...
- 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 no ...
- Leetcode671.Second Minimum Node In a Binary Tree二叉树中的第二小结点
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中 ...
- LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
- 【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 ...
- Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)
671. 二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的 ...
- 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 ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
随机推荐
- 第一章 走进Python
目标 了解Python的历史 了解Python的特征 了解Python的应用 掌握Linux下Python开发环境的搭建 理解Windows下Python环境搭建 案例 安装Python,写出第一个P ...
- SQL关联查询中on与where
微信公众号:刺刺刺猬的优雅 前段时间,做一个查询,打算用left join查询存在于A表但不存在于B表记录,但怎么查都不对,原因是把所有filter全部放在了where语句中,因此回头看了资料,记录一 ...
- Android: 利用SurfaceView绘制股票滑动直线解决延迟问题
1.背景介绍 最近项目要绘制股票走势图,并绘制能够跟随手指滑动的指示线(Indicator)来精确查看股票价格和日期.如下图所示: 上图中的那条白色直线就是股票的指示线,用来跟随手指精确确定股票的时间 ...
- bzoj 3012: [Usaco2012 Dec]First! Trie+拓扑排序
题目大意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最小的串,并输出这些串.n <= 30,000 , m <= 300,0 ...
- webpy+nginx+uwsgi安装配置
转:(1)安装Nginx1.1 下载nginx-1.0.5.tar.gz并解压1.2 ./configure (也可以增加--prefix= path指定安装路径)此时有可能会提示缺少pcre支持,如 ...
- POJ2785(upper_bound)
#include"cstdio" #include"algorithm" using namespace std; ; int A[MAXN],B[MAXN], ...
- flume入门之一:flume 安装及测试
http://flume.apache.org/ flume下载:http://mirror.bit.edu.cn/apache/flume/1.7.0/apache-flume-1.7.0-bin. ...
- CSDN优秀博客连接,博客之星连接。
点击链接 获得[红杏出墙]插件,FQ上网无压力!谷歌搜索无压力! 2013年度CSDN十大博客之星 TOP 作者 专注领域 博客地址 邹晓艺 机器学习及算法 zouxy09 2 王然 潜在的集大成者 ...
- JAVA基础知识总结2(语法基础)
关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:暂时还未规定为关键字的单词,保留准备日后要使用的单词. 标识符:开发人员程序中自定义名词,比如类名,变量名,函数名. PS:1.不能使用关键字. ...
- centos 7之文件共享
一,安装过程 1.在VirtualBox的软件菜单里面选择 “设备”--“存储”,添加VBoxGuestAdditions.iso(在VirtualBox目录下). 2.在桌面上出现一个光驱 ...