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. 转: Qt信号槽实现原理 清晰明了

    转: https://blog.csdn.net/perfectguyipeng/article/details/78082360 本文使用 ISO C++ 一步一步实现了一个极度简化的信号与槽的系统 ...

  2. Ubuntu 18.04 下 Redis 环境搭建

    一.安装 Redis ① 下载 wget http://download.redis.io/releases/redis-3.2.8.tar.gz ② 解压 tar -zxvf redis-3.2.8 ...

  3. SQL Server 主键及自增长列的修改

    一.对主键的修改 主键值都会带有主键约束,当执行update操作或是其他操作的时候就会受到限制无法修改,解决的方法是:取消主键约束->删掉主键列->插入修改后的主键值. (1)取消主键约束 ...

  4. 菜鸟使用MySQL存储过程and临时表,供新手参考,请高手斧正

    因为公司最近的一个项目,第一次用到了MySQL(5.10版本),之前听传说MySQL很厉害的样子,因为开源而神奇,但是现在用起来, 感觉并不好啊!我知道是我水平太down,呜呜呜,请各路神仙略施小技, ...

  5. Python,是什么让我遇见你

    有些不可思议,作为一个曾学了半年C语言还挂了的计算机学渣,我竟然选择了Python这门计算机语言课.事实上,我是有过犹豫的,毕竟知道自己不擅长这方面的学习还选这门课,这种行为确实看起来有点傻傻的.但最 ...

  6. C++各种优化

    目录 1.快速结束程序 2.register 3.inline 4.位运算 5.少用或不用STL 6.快读快写 准备工作 超慢cin cout 输出 输入 神一般的快读快写 对比 7.小技巧 8.co ...

  7. MyBatis工具类

    package cn.word.util; import java.io.IOException;import java.io.InputStream;import java.util.Enumera ...

  8. StringBuffer&StringBuilder

    对字符串修改时,用到StringBuffer&StringBuilder,能够多次修改对象并且不产生新的未使用对象 StringBuilder线程不安全(不能同步访问),速度有优势,多数情况下 ...

  9. python学习笔记:2.python基础

    4.27 01,pycharm 安装使用. 011,昨日内容回顾.     编译型:         将代码一次性全部编译成二进制,然后运行.         优点:执行效率高.         缺点 ...

  10. Beta冲刺四

    1.团队TSP 团队任务 预估时间 实际时间 完成日期 对数据库的最终完善 120 150 12.2 对学生注册功能的完善--新增触发器 150 140 11.29 对教师注册功能的完善 150 13 ...