给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 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二叉树中的第二小结点的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

    671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...

  5. 【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 ...

  6. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  7. [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 ...

  8. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

  9. 【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 ...

随机推荐

  1. neo4j的搭建和实例使用

    一. 简介 neo4j是当今最流行的图数据库,基于 节点+关系 的架构,保存了图形数据的基本元素.同时,数据库也支持通过基础数据元素和独特的CQL查询语法,快速方便的检索.构建复杂的图表关系结果. 二 ...

  2. matlab-选择-循环-函数

    1 选择 3 循环 break 3 函数

  3. mac版pycharm的字体和行间距设置

  4. parameter–precharge, tRCD and tRAS

    以下描述来自wikipeida : https://en.wikipedia.org/wiki/Synchronous_dynamic_random-access_memory 几点总结: (1) 每 ...

  5. BZOJ 1933 [Shoi2007]Bookcase 书柜的尺寸

    神奇的dp优化. 考虑6维状态的dp,分别表示三行高和宽,显然MLE&&TLE. 把高排个序,从大到小往架上放,那么若不是重开一行便对高度没有影响. 然后求出宽度的sum,dp[i][ ...

  6. Angular本地数据存储LocalStorage

    //本地存储数据===================================.factory('locals',['$window',function($window){ return{ / ...

  7. SQL Server作业的备份

    作业备份,不是备份数据库,是备份作业.我的方法是把作业导出成文件备份起来,因为当你服务器维护的多了的时候很多你的作业 就很成问题,很麻烦.最好能够作业实现同步,这个也是第一步,保存成文件,之后个人设想 ...

  8. SPSS分析技术:CMH检验(分层卡方检验);辛普森悖论,数据分析的谬误

    SPSS分析技术:CMH检验(分层卡方检验):辛普森悖论,数据分析的谬误 只涉及两个分类变量的卡方检验有些时候是很局限的,因为混杂因素总是存在,如果不考虑混杂因素,得出的分析结论很可能是谬误的,这就是 ...

  9. python运~算~~符!!!!!!!!!!!

    目录: 算术运算, 用于加减乘除等数学运算 赋值运算,用于接收运算符或方法调用返回的结果 比较运算, 用于做大小或等值比较运算 逻辑运算,用于做 与.或.非运算 位运算, 用于二进制运算 每种运算中所 ...

  10. UVAL3700

    Interesting Yang Hui Triangle 题目大意:杨辉三角第n + 1行不能整除p(p是质数)的数的个数 题解: lucas定理C(n,m) = πC(ni,mi) (mod p) ...