Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值。二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有。
分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要找到左右子树中比跟节点大的最小值就可以了。对于这个题目,还是考察的二叉树的搜索,第一印象是BFS。使用一个辅助队列存储遍历过程中的节点,遍历过程中,如果节点的值比结果中第二小的值还大,就不用继续把该节点及其所有的子节点入队了,这样可以节省时间。这里的辅助队列使用的是deque,在两端存取数据的复杂度都是O(1),List取出第一个元素的复杂度是O(n)。代码如下:
# 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
"""
from collections import deque
if not root:
return -1
stack = deque([root])
ret = [root.val]
while stack:
node = stack.popleft()
if node:
if node.val > ret[-1]:
if len(ret) < 2:
ret.append(node.val)
else:
if ret[0] < node.val < ret[1]:
ret[1] = node.val
stack.append(node.left)
stack.append(node.right)
return ret[1] if len(ret) == 2 else -1
每次做完都要去讨论区看看有没有亮瞎眼的解法,把DFS的代码贴在下面,思路是一样的,不过看着更简洁了:
class Solution(object):
def findSecondMinimumValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# Time: O(n)
# Space: O(lgn)
res = [float('inf')]
def traverse(node):
if not node:
return
if root.val < node.val < res[0]:
res[0] = node.val
traverse(node.left)
traverse(node.right)
traverse(root)
return -1 if res[0] == float('inf') else res[0]
这里需要说一下代码中的float('inf'),以前倒是真没用到这里,这是Python中代表正无穷的写法,负无穷就是float('-inf')了,又了解了一个新内容,嘿嘿。
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 ...
- 【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/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- [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 ...
- 【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 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- Python3常用学习网站总结(随时更新)
Python资源大全 http://python.jobbole.com/84464/ https://github.com/jobbole/awesome-python-cn scrapy: h ...
- 从零开始配置TypeScript + React + React-Router + Redux + Webpack开发环境
转载请注明出处! 说在前面的话: 1.为什么不使用现成的脚手架?脚手架配置的东西太多太重了,一股脑全塞给你,我只想先用一些我能懂的库和插件,然后慢慢的添加其他的.而且自己从零开始配置也能学到更多的东西 ...
- HDU1403Longest Common Substring
明天写 超时代码: #include<cstdio> #include<cstdlib> #include<iostream> #include<cstrin ...
- Linux向文件添加内容的几种方法
例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 打开终端,输入vi test.txt 回车,按a或i进入编辑模式, ...
- UWP xaml 圆形头像
圆形头像 去掉黑边 拖动打开图形 圆形头像 现在很多软件都喜欢使用圆形头像 win10 uwp使用圆形头像很简单 <Ellipse Width="200" Height=&q ...
- VS2017生成解决方案报错,提示对路径的访问被拒绝
目前我用的vs2017的版本是15.3.5.生成解决方案有时会提示如下: 开始以为是权限的问题,找到相应的目录设置everyone权限,再次生成还是不行.重启VS试了下,还是不行. 最后无奈重启下电脑 ...
- 假如时光倒流,我会这么学习Java
回头看看, 我进入Java 领域已经快15个年头了, 虽然学的也一般, 但是分享下我的心得,估计也能帮大家少走点弯路. [入门] 我在2001年之前是C/C++阵营, 有C和面向对象的基础, 后来转到 ...
- oracle导不出空表的解决办法
1.先进行表分析(一定要执行此步,否则查询空表可能不准确) select 'analyze table '||table_name||' compute statistics;' from user_ ...
- CentOS本地yum源配置
现有一台处在局域网的linux服务器,无法ping通外网,本文是关于本地yum源的配置 环境 : CentOS 6.5 一 .挂载CentOS镜像文件 (1) 创建挂载文件夹,若此文件夹已存在可忽略 ...