给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 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. Window和Mac下端口占用情况及处理方式

    1. 在Mac下端口占用的情况: 找到占用的进程并杀掉: 1.查看端口占用进程 sudo lsof -i :8880 可以看到进程的PID 2.杀掉进程 sudo kill -9 4580(4580为 ...

  2. Excel常用函数总结

    Excel常用函数总结 2016-10-28 Kevin 叼着奶瓶撩妹 1. VLOOKUP函数 常见形式 问题描述:将下图中G列的数据根据学生的姓名填充到D列. 公式解析: =VLOOKUP(A2, ...

  3. JDK源码阅读--HashMap

    public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, ...

  4. div 和 span 标记

    div 一般和 css 配合使用  <div>是一个块元素 span  也是和 css 配合使用 <span>是一个行内元素 标记嵌套是  一般是块元素嵌套行内元素 1 块元素 ...

  5. Cesium官方教程13--Cesium和Webpack

    原文地址:https://cesiumjs.org/tutorials/cesium-and-webpack/ Cesium 和 Webpack Webpack是非常强大非常流行的JavaScript ...

  6. PAT甲级——A1083 List Grades

    Given a list of N student records with name, ID and grade. You are supposed to sort the records with ...

  7. NYOJ--311(完全背包)

    题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=311 分析:这里就是一个完全背包,需要注意的就是初始化和最后的输出情况        dp[j ...

  8. Jmeter安装与配置(第一篇)

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文 ...

  9. vue 组件内引入外部在线js、css

    参考:https://blog.csdn.net/u010881899/article/details/80895661 例:引入element-ui js: mounted() { const oS ...

  10. MYSQL错误代码和消息

    服务器错误信息来自下述源文件: ·         错误消息信息列在share/errmsg.txt文件中.“%d”和“%s”分别代表编号和字符串,显示时,它们将被消息值取代. ·         错 ...