Leetcode671.Second Minimum Node In a Binary Tree二叉树中的第二小结点
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。

题目应该少说了树的节点值都是大于等于0的
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
if(root == NULL)
return -1;
int x = GetAns(root ->left, root ->val);
int y = GetAns(root ->right, root ->val);
if(x == -1 && y == -1)
{
return -1;
}
else if(y == -1)
{
return x;
}
else if(x == -1)
{
return y;
}
return min(x, y);
}
int GetAns(TreeNode* root, int k)
{
if(root == NULL)
return -1;
if(root ->val != k)
return root ->val;
else
{
int x = GetAns(root ->left, k);
int y = GetAns(root ->right, k);
if(x == -1 && y == -1)
{
return -1;
}
else if(y == -1)
{
return x;
}
else if(x == -1)
{
return y;
}
return min(x, y);
}
}
};
Leetcode671.Second Minimum Node In a Binary Tree二叉树中的第二小结点的更多相关文章
- [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二叉树中第二小的节点 (C++)
题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where eac ...
- [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点
Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...
- 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 ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- [Swift]LeetCode671. 二叉树中第二小的节点 | 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 ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- 【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 ...
随机推荐
- System.Web.Mvc.FileContentResult.cs
ylbtech-System.Web.Mvc.FileContentResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...
- Netty SimpleChannelInboundHandler和ChannelInboundHandler区别
一般用netty来发送和接收数据都会继承SimpleChannelInboundHandler和ChannelInboundHandlerAdapter这两个抽象类,那么这两个到底有什么区别呢? 在客 ...
- google移动版针对智能手机、非智能手机的蜘蛛的User-agent
非智能手机蜘蛛的User-agent有以下两个 SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2. ...
- <每日一题>题目28:生成随机的测验试卷(单选题)
#项目:生成随机的测验试卷文件 import random #资料库 capitals = {'北京市':'京','上海市':'沪','天津市':'津','重庆市':'渝','河北省':'冀','山西 ...
- Ubuntu 卸载nvidia驱动
1.切换为集成显卡 如果没有,那么先切换到字符界面 2.卸载驱动 sudo apt-get --purge remove nvidia* sudo apt autoremove To remove C ...
- 廖雪峰Java10加密与安全-2加密算法-1URL编码
1.URL编码 URL编码是浏览器发送数据给服务器时使用的编码. 如通过百度搜索美女: 编码前:https://www.baidu.com/s?wd=美女 编码后:https://www.baidu. ...
- vue.js_05_vue.js的过滤器
1.过滤器的定义和使用 实现:将页面的中的单纯替换成,用户传来的文字. 全局过滤器:所有的Vue对象都可以使用 <body> <div id="app"> ...
- spring cloud深入学习(十)-----配置中心和消息总线(配置中心终结版)
如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...
- MySql存储过程批量删除多个数据库中同名表中的指定字段
1. 创建存储过程batchDeleteField:删除所有名称为"MyDB_"开头的数据库中的指定字段 -- ---------------------------- -- Pr ...
- 《DSP using MATLAB》Problem 7.32
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...