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 ...
随机推荐
- Gen8折腾日记
(2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2016年7月7日,可惜在博客园这边不能修改发布时间.) 放假伊始,老大订购了两台服务器,一台是Dell的R630,用于其他 ...
- codeforces 1131D-Gourmet choice
传送门:QAQQAQ 题意:有两个数组,一个数组有n个数,另一个数组有m个数.s[i][j]表示第一个数组第i个数和第二个数组第j个数的大小关系,要求构造出一种方案,使条件成立. 先考虑没有等于号的情 ...
- 让footer固定在页面底部(CSS-Sticky-Footer)
让footer固定在页面底部(CSS-Sticky-Footer) 这是一个让网站footer固定在浏览器(页面内容小于浏览器高度时)/页面底部的技巧.由HTML和CSS实现,没有令人讨厌的h ...
- Js中获取时间 new date()的用法
Js中获取时间 new date()的用法 获取时间: var myDate = new Date();//获取系统当前时间 myDate.getYear(); //获取当前年份(2位) myDate ...
- Ubuntu 14.04 Ruby 2.3.3 安装
在Ubuntu 14.04通过下载Ruby源码包进行安装. 第一步,更新apt-get sudo apt-get update 通过apt-get安装ruby依赖 sudo apt-get insta ...
- Create STKNetDiskC Instance Error
关于Create STKNetDiskC Instance Error错误的解决方法 这个错误可能出现在: AM8 附件直接存网盘的时候 报错 解决方法步骤如下: 在出现该错误的机器上,先将AM及 m ...
- HZOI20190722 B visit 组合数+CRT合并
题目:https://www.cnblogs.com/Juve/articles/11226266.html solution: 30%:dp 设dp[k][i][j]表示经过k时间,在(i,j)的方 ...
- win 7下安装mysql zip格式
mysql 下载地址:https://dev.mysql.com/downloads/mysql/5.5.html#downloads 下载的mysql格式为zip: 下载完成放在除C盘以外的盘. 一 ...
- AppbarLayout的简单用法
在许多App中看到, toolbar有收缩和扩展的效果, 例如: appbar.gif 要实现这样的效果, 需要用到: CoordinatorLayout和AppbarLayout的配合, 以及实 ...
- SpringMVC配置顺序的问题
1:web.xml:web应用一经加载,先来找他 1):指明applicationContext的位置 2):引入spring监听,ContextLoaderListe ...