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.

Example 1:

Input:
2
/ \
2 5
/ \
5 7 Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

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

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def findSecondMinimumValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.ans=float('inf')
min1=root.val def dfs(node):
if node:
if min1<node.val<self.ans:
self.ans=node.val
elif node.val==min1:
dfs(node.left)
dfs(node.right) dfs(root)
return self.ans if self.ans<float('inf') else -1

  

[LeetCode&Python] Problem 671. Second Minimum Node In a Binary Tree的更多相关文章

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

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

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

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

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

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

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

  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 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

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

随机推荐

  1. 新建Eclipse的web工程目录结构和MyEclipse相似的设置

    新建一个文件名为“org.eclipse.wst.web.prefs”的文件 里面设置的内容如下: “webContent=webRoot%proj%\bin= %webRoot%\WEB-INF\c ...

  2. python中list,tuple,dict,set等深浅拷贝的问题记录

    对于字典.元祖.列表 而言,进行赋值.浅拷贝和深拷贝时,其内存地址的变化是不同的. 1.赋值 赋值,只是创建一个变量,该变量指向原来内存地址,如: 1 2 3 n1 = {"k1" ...

  3. mysql Navicat客户端

    Navicat是一个用来操作多种数据库的客户端. 可应用操作系统:Windows.macOS.Linux. 可应用 Navicat 产品:Navicat for MySQL.Navicat for P ...

  4. 显式拥塞通告(ECN)及其在Linux上的实现

    1 ECN简介 首先看看ECN握手报文的特点,根据RFC3168,ECN握手报文IP头部不能够设置ECT和CE位的 SYN报文TCP标志字段的CWR和ECE位被置1 SYN-ACK报文的CWR位被置0 ...

  5. FTP 代理服务器实现

    本文将在Linux环境下实现一个简单的FTP代理服务器,主要内容涉及FTP主动/被动模式和简单的Socket编程. 1. 主动模式和被动模式 FTP有两种模式,即主动模式(Active Mode)和被 ...

  6. Python 随笔-1

    python的发展史: python 2.7            July 3,2010  目前业内主流使用的工业版本 主讲3.0 32bit = 内存的最大寻址空间为2*32    4G的空间 6 ...

  7. 201671010142 java内部类

    内部类可以拥有private访问权限.protected访问权限.public访问权限及包访问权限. 局部内部类是定义在一个方法或者一个作用域里面的类,它和成员内部类的区别在于局部内部类的访问仅限于方 ...

  8. 记录一次配置golang服务器端口

    之前配置程序监听端口,地址都写成IP+:Port的格式,然而一直调试不同,也找不出问题. 后来,参考博客https://blog.csdn.net/yoie01/article/details/214 ...

  9. Python标准库01正则表达式

    在学习网络爬虫的过程中,需要抓取网页的评论数,涉及到正则表达式,便顺便看了看.正则表达式是文字处理中常用的工具. 1正则表达式的常用字符串 .       任何单个字符 [] 字符集对单个字符给出取值 ...

  10. eclipse 打开时一闪而过解决办法

    编辑文件:eclipse.ini,在 -vmargs 上一行添加: -vmC:/Program Files/Java/jdk1.8.0_131/jre/bin “C:/Program Files/Ja ...