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/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
随机推荐
- codeforces 637B B. Chat Order(map,水题)
题目链接: B. Chat Order time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- leetcode 1 Two Sum(查找)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- Struts2 - 配置文件详解
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- Mybatis generator配置文件及说明
项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...
- debian服务查询
1.查询 用root身份执行service --status-all查看所有服务的状态 "+" started "-" stopped "?" ...
- HDOJ1171(多重背包)
#include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a>b)?a:b + ...
- Python:字典的pop()方法
pop():移除序列中的一个元素(默认最后一个元素),并且返回该元素的值. 一)移除list的元素,若元素序号超出list,报错:pop index out of range(超出范围的流行指数): ...
- rails中一个窗体多个模型——fields_for
详细参考 http://railscasts.com/episodes/73-complex-forms-part-1中part-1.2.3部分 借助field_for可以生成表单来处理两个或更多模型 ...
- UML核心元素--包
包是一种容器,如同文件夹一样,将某些信息分类,形成逻辑单元.包可以容纳任何UML元素,例如用例.业务实体.类图等,也包括子包. 一.分包原则: (1)高内聚:被分入同一个包的元素相互联系紧密,伸至不可 ...
- C#事件触发机制
C#的事件触发机制,类似于c++的回调函数机制 我先简单说一下,委托和事件的实质,后期再重开一篇博文来详细说 委托:指向方法的指针,类似于C的函数指针 事件:是一个可以存放0个或多个方法指针的数据结构 ...