题目在这里,要求一个二叉树的倒数第二个小的值。二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有。

分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要找到左右子树中比跟节点大的最小值就可以了。对于这个题目,还是考察的二叉树的搜索,第一印象是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的更多相关文章

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

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

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

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

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

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

  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. leetCode没那么难啦 in Java (二)

    介绍    本篇介绍的是标记元素的使用,很多需要找到正确元素都可以将正确元素应该插入的位置单独放置一个标记来记录,这样可以达到原地排序的效果. Start 27.RemoveElement 删除指定元 ...

  2. bzoj1143 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  3. 笨鸟先飞之ASP.NET MVC系列之过滤器(02授权过滤器)

    授权过滤器 概念介绍 在之前的文章中我们已经带大家简单的了解了下过滤器,本次我们开始介绍授权过滤器. 我们之前提到过授权过滤器在认证过滤器之后,其他过滤器和方法被调用之前运行,而授权过滤器和它名字的含 ...

  4. Linux vi 退出&保存/不保存

    无论是否退出 vi,均可保存所做的工作.按 ESC 键,确定 vi 是否处于命令模式. 操作   键入 保存,但不退出vi                          :w 保存并退出vi    ...

  5. 错误:Cannot set property 'innerHTML' of null

    360浏览器代码编辑器里提示错误:Cannot set property 'innerHTML' of null 原因是代码执行时要调用的内容不存在

  6. webpack 的使用2

    实际项目中的配置 要加__dirname 不然会报错 注意path  /dist 前不要加点 结果 将两个文件打包在一起 结果 传入对象 并且单独打包 name为key 加上本次打包的hash has ...

  7. Jquery跨域读取城市天气预报信息

    最新项目中遇到一个问题,页面需要显示一些天气信息,但是部署网站的服务器没连接外网,只有客户端的电脑能连外网,于是想用js去实现这个功能. 刚开始找了一些方法,单独在浏览器中可以使用,但是在项目中运行的 ...

  8. 前端要革命?看我在js里写SQL

    在日新月异的前端领域中,前端工程师能做的事情越来越多,自从nodejs出现后,前端越来越有革了传统后端命的趋势,本文就再补一刀,详细解读如何在js代码中执行标准的SQL语句 为什么要在js里写SQL? ...

  9. 基于Redis位图实现系统用户登录统计

    项目需求,试着写了一个简单登录统计,基本功能都实现了,日志数据量小.具体性能没有进行测试~ 记录下开发过程与代码,留着以后改进! 1. 需求 1. 实现记录用户哪天进行了登录,每天只记录是否登录过,重 ...

  10. Git命令(1)

    windows中文乱码: http://www.cnblogs.com/Gukw/archive/2012/01/16/2323417.html 学习地址 :http://www.liaoxuefen ...