[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 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的更多相关文章
- 【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 ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- 671. Second Minimum Node In a Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- 6354 Everything Has Changed
Edward is a worker for Aluminum Cyclic Machinery. His work is operating mechanical arms to cut out d ...
- 老版本db2这里下
https://www-01.ibm.com/support/docview.wss?uid=swg27007053 db2 10.5.10.1.9.x等 下最新FIX版即可
- javascript 判断质数
1.判断n是否为number类型,是否为整数,是否小于2: 2.若n == 2返回true: 3.从3至n的算术平方根(square)之间的奇数,如果n取余为0,则不是奇数. var isPrime ...
- 详解Linux下swig 3.0.12的手动安装过程
详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...
- USM-V1.0
ADSP-BF512 :Low Power Blackfin with Consumer Devices Connectivity The ADSP-BF512 is the low cost ent ...
- Unity中Terrain中刷出的树木模糊解决办法
Billboard Start 该项用于设定摄像机将树渲染为广告牌的距离.
- 数独游戏 js
规则:玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字:保证每一行,每一列,每个宫的数字刚好含1-9,并且不重复. 一.步骤: 生成格子 —— 生成9×9满足规则的数字 —— 置空一定 ...
- Eclipse控制台输出log日志中文乱码
今天在工作中,调试程序的时候突然发现控制台的log日志,输出的中文全都是乱码. 看到这就在想,这是项目编码还是log.xml编码配置被改掉了呢?于是统统检查了一遍发现所有的编码格式都是统一用的utf- ...
- windows环境下,spring boot服务使用docker打包成镜像并推送到云服务器私有仓库
最近在淘宝上学习springcloud教程,其中有几节课是讲解讲本地springboot服务打包成镜像并推送到云服务器私有仓库,但是教程里面用的事Mac环境,我的是Windows环境,而且课程里面没有 ...
- 展示金额的方法(1元-->1.00元)
public static String showMoneyByTwoDecimal(String account) { DecimalFormat doubleFormatter = new Dec ...