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. Oracle查看存储过程最后编辑时间

    场景:我们在实现一个需求编写存储过程时,在正式上线前,总会有多个修改版本,时间一长可能发现一个过程甚至有5个以上的版本,如果没有添加注释自己都分不清哪个版本是最新的,这时就可以通过查看该存储的最后编辑 ...

  2. java 编译

    package javacodeforstudy.testcode; public class Helloworld{ public static void main(String[] args) { ...

  3. 使用idea搭建maven项目,结果spring-mybatis.xml文件报红“Cannot resolve file 'jdbc.properties' less... (Ctrl+F1) Inspection info:Spring XML model validation”

    错误如下图: 解决方案: 暂时只是使用右下角的highlightinglevel来屏蔽,拖动到最左边就行了

  4. laravel的测试工具debug安装:

    在项目根目录执行: composer require barryvdh/laravel-debugbar --dev

  5. ClientDataSet使用locate或Filter定位到字段为空值的记录

    场景,程序想检查是否存在某个字段的值是空的,如果存在,则不允许增加记录,否则允许增加记录. 解决这个问题,我一开始用了两种错误的方法 if not clientdataset.locate('AFie ...

  6. git 删除与撤回

    Git 删除与撤回 标签: git 版本管理 删除文件 撤回删除操作 2017年01月13日 22:56:27786人阅读 评论(0) 收藏 举报  分类: Git(4)  版权声明:本文为博主原创文 ...

  7. Trojan.Backdoor分析

    总结:这是一个HTTP的后门,以安装(-in)||移除(-re)||配置(-c)为目的运行此程序时, 必须指定abcd为最后一个参数. 安装时他会把自身拷贝到%SYSTEMROOT%\WINDOWS\ ...

  8. http协议与https协议的前世今生

    一.Http与Https的区别: HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头 HTTP 是不安全的,而 HTTPS 是安全的 HTTP 标准端口是80 ...

  9. 使用docker安装sentry

    一.安装docker yum -y install docker 更换docker镜像源为中科大的 在配置文件/etc/docker/daemon.json中加入 { "registry-m ...

  10. IDEA解决SVN更新冲突

    在有冲突的文件上右键-> subversion ->resolve Text Confict->merge 将代码合并.