作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/

题目描述

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s value is the smaller value among its two sub-nodes.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.

If no such second minimum value exists, output -1 instead.

  1. Example 1:
  2. Input:
  3. 2
  4. / \
  5. 2 5
  6. / \
  7. 5 7
  8. Output: 5
  9. Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

  1. Input:
  2. 2
  3. / \
  4. 2 2
  5. Output: -1
  6. Explanation: The smallest value is 2, but there isn't any second smallest value.

题目大意

找二叉树中的第二小的结点值。并且给该二叉树做了一些限制,比如对于任意一个结点,要么其没有子结点,要么就同时有两个子结点,而且父结点值是子结点值中较小的那个,当然两个子结点值可以相等。

解题方法

找出所有值再求次小值

使用了一个中序遍历,把所有的值放入到set里,然后我们先找最小值,然后删除掉它之后,再求一次最小值就是次小值。

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution(object):
  8. def findSecondMinimumValue(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: int
  12. """
  13. self.res = set()
  14. self.inOrder(root)
  15. if len(self.res) <= 1:
  16. return -1
  17. min1 = min(self.res)
  18. self.res.remove(min1)
  19. return min(self.res)
  20. def inOrder(self, root):
  21. if not root:
  22. return
  23. self.inOrder(root.left)
  24. self.res.add(root.val)
  25. self.inOrder(root.right)

遍历时求次小值

因为根节点的值一定是比两个子树的值小的,所以我们知道了所有的值的最小值一定是root的值。如何找到第二小的值呢?可以使用一个变量,时刻保存现在遇到的比最小值大并且比次小值小的值作为第二小的值。

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution(object):
  8. def findSecondMinimumValue(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: int
  12. """
  13. if not root: return -1
  14. self.res = float("inf")
  15. self.min = root.val
  16. self.inOrder(root)
  17. return self.res if self.res != float("inf") else -1
  18. def inOrder(self, root):
  19. if not root:
  20. return
  21. self.inOrder(root.left)
  22. if self.min < root.val < self.res:
  23. self.res = root.val
  24. self.inOrder(root.right)

日期

2018 年 1 月 31 日
2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)的更多相关文章

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

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

  4. [LeetCode&Python] Problem 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 ...

  5. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

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

  7. 671. Second Minimum Node In a Binary Tree 非递减二叉树中第二小的元素

    [抄题]: Given a non-empty special binary tree consisting of nodes with the non-negative value, where e ...

  8. 671. Second Minimum Node In a Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

随机推荐

  1. 嵌入式Linux利用ppp实现4G模块联网

    https://blog.csdn.net/qq361294382/article/details/52136126 https://blog.csdn.net/qq361294382/article ...

  2. Excel-判断一个文本字符串中是否包含数字! 判断一个文本字符串是否是纯汉字!

    0.判断一个文本字符串中是否包含数字!/判断一个文本字符串是否是纯汉字! 公式=IF(LENB(A1)=2*LEN(A1),"都是汉字","含有非汉字字符") ...

  3. WPS表格数据透视表的美化和布局

    设计--分类汇总--在组的底部显示所有分类汇总   把二级分类单独放在一类中 设计--报表布局--以表格形式显示   快速调整表格的外观 分析--+/-按钮   设置字段的数字格式以万元为单位 选中任 ...

  4. 在前端页面中使用Markdown并且优化a标签

    近期在自己的项目中加入了对 Markdown 语法 的支持,主要用到的是markedjs这个项目.该项目托管在github上,地址为:https://github.com/markedjs/marke ...

  5. 使用GitHub Action进行打包并自动推送至OSS

    GitHub Action 是 GitHub 于 2018 年 10 月推出的一个 CI\CD 服务. 官方文档:https://docs.github.com/cn/actions CI\CD 持续 ...

  6. 学习Vue源码前的几项必要储备(二)

    7项重要储备 Flow 基本语法 发布/订阅模式 ES6+ 语法 原型链.闭包 函数柯里化 event loop 接上讲 聊到了ES6的几个重要语法,加下来到第四点继续开始. 4.原型链.闭包 原型链 ...

  7. 【SpringBoot】几种定时任务的实现方式

    SpringBoot 几种定时任务的实现方式 Wan QingHua 架构之路  定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java ...

  8. Linux基础命令---elinks文本浏览器

    elinks elinks指令是一个纯文本格式的浏览器,支持颜色.表格.鼠标.菜单操作. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法       ...

  9. java-阿里邮件推送服务开发 -- 发送邮箱验证码

    参考文档: 如何在 DNS 服务器上配置域名:https://help.aliyun.com/knowledge_detail/39397.html?spm=5176.2020520150.102.d ...

  10. apply 和 call 的区别

    相同点: 都能够改变方法的执行上下文(执行环境),将一个对象的方法交给另一个对象来执行,并且是立即执行 不同点: call方法从第二个参数开始可以接收任意个参数,每个参数会映射到相应位置的func的参 ...