给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。

给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。

题目应该少说了树的节点值都是大于等于0的

  1. class Solution {
  2. public:
  3. int findSecondMinimumValue(TreeNode* root) {
  4. if(root == NULL)
  5. return -1;
  6. int x = GetAns(root ->left, root ->val);
  7. int y = GetAns(root ->right, root ->val);
  8. if(x == -1 && y == -1)
  9. {
  10. return -1;
  11. }
  12. else if(y == -1)
  13. {
  14. return x;
  15. }
  16. else if(x == -1)
  17. {
  18. return y;
  19. }
  20. return min(x, y);
  21. }
  22. int GetAns(TreeNode* root, int k)
  23. {
  24. if(root == NULL)
  25. return -1;
  26. if(root ->val != k)
  27. return root ->val;
  28. else
  29. {
  30. int x = GetAns(root ->left, k);
  31. int y = GetAns(root ->right, k);
  32. if(x == -1 && y == -1)
  33. {
  34. return -1;
  35. }
  36. else if(y == -1)
  37. {
  38. return x;
  39. }
  40. else if(x == -1)
  41. {
  42. return y;
  43. }
  44. return min(x, y);
  45. }
  46. }
  47. };

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. 关于promise的用法

    promise是一个对象,里面保存着某个未来才会结束的事件,通常是一个异步事件. promise对象的两个特点: 1.对象的状态不受外界影响:pending(进行中) fulfilled(已成功) r ...

  2. lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(一) 简介

    一. 为什么这样构架 1. 系统高可用性 2. 系统可扩展性 3. 负载均衡能力 LVS+keepalived能很好的实现以上的要求,keepalived提供健康检查,故障转移,提高系统的可用性!采用 ...

  3. Elasticsearch template学习

    Elasticsearch template Elasticsearch存在一个关键问题就是索引的设置及字段的属性指定,最常见的问题就是,某个字段我们并不希望ES对其进行分词,但如果使用自动模板创建索 ...

  4. 05_springmvc参数绑定

    一.参数绑定的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收.而不 ...

  5. springboot自己实现mysql主从数据切换机制

    在很多公司都是实现了数据的读写分离,所谓的读写分离,就是写的时候从主库 ,然后从库就会从主库中复制过去,这样就会形成了数据的读写分离,然而在很多场景是适用的,那么我们怎么做呢,可以利用aop 加注解的 ...

  6. day65作业

    有 红.黄.蓝 三个按钮,以及一个200x200矩形框box,点击不同的按钮,box的颜色会被切换为指定的颜色 <body> <div id="app"> ...

  7. 你所不知道的Mac截图的强大

    Mac的截图功能扩展功能很强大的,不要用QQ那个COM+Ctrl+A弱爆了的截图了~ 首先说一下两种截图 1.Command+shift+3:全屏截图,保存截图到桌面 2.Command+shift+ ...

  8. Vue 将本地图片上传到阿里云

    一.获取服务器通行证(即获取AccessKey和accessKeySecret) getAccess () { let that = this let url = '服务器地址' let params ...

  9. hibernate 多表关联外键问题无法截断表的解决办法

    目前只有一个办法 就是手动清除其他表的外键关联,然后在做一个小swing单独去操作这个表,而不运行主控方相关的代码 当web运行后,外键会再次设置好

  10. C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客

    原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...