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:

  1. Input:
  2. 2
  3. / \
  4. 2 5
  5. / \
  6. 5 7
  7.  
  8. Output: 5
  9. Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

  1. Input:
  2. 2
  3. / \
  4. 2 2
  5.  
  6. Output: -1
  7. Explanation: The smallest value is 2, but there isn't any second smallest value.

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. def findSecondMinimumValue(self, root):
  10. """
  11. :type root: TreeNode
  12. :rtype: int
  13. """
  14. self.ans=float('inf')
  15. min1=root.val
  16.  
  17. def dfs(node):
  18. if node:
  19. if min1<node.val<self.ans:
  20. self.ans=node.val
  21. elif node.val==min1:
  22. dfs(node.left)
  23. dfs(node.right)
  24.  
  25. dfs(root)
  26. 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. 关于sqlserver数据库max()方法的使用

    1.传送门:http://www.codesky.net/article/201009/144935.html 2.copy自传送门里的内容: max(字符串内容): 说明对字符型数据的最大值,是按照 ...

  2. Vue的学习

    1.Vue是什么 参考Vue官网,一套用于构建用户界面的渐进式框架. 2.什么是渐进式框架 引用大神的话:“它给你提供足够的optional,但并不主张很多required,也不多做职责之外的事!这就 ...

  3. shell 下生成使用UUID

    #!/bin/bash psd="/proc/sys/kernel/random/uuid" echo $(cat $psd)UUID=$(cat /proc/sys/kernel ...

  4. SpringBoot配置Swagger实例(POST接收json参数)

    工程目录结构: 首先,引入jar包,只需要以下两个即可 <dependency> <groupId>io.springfox</groupId> <artif ...

  5. mysql 查询时指定校对规则

    为了能在查询时忽略字段大小写,又不想修改数据表的属性配置,就在SQL语句中做了修改.结果在alibaba druid 执行时报错 com.alibaba.druid.sql.parser.Parser ...

  6. git冲突解决的几种办法

    文章目录 git stash 栈 放弃本地修改 撤销分支 强行冲掉之前的分支 删除分支 git stash 栈 git stash git pull git stash pop 当pull出现冲突时 ...

  7. windwos安装docker步骤

    参考 https://www.linuxidc.com/Linux/2016-07/133506.htm

  8. 外贸站全球网速测试+免费CDN使用教程

    关于外贸网站速度测试,以前一全老师(www.yiquanseo.com)也讲到过,但是在那篇文章中推荐给大家的两个测试网站(https://developers.google.com/speed/pa ...

  9. datatable处理gridview筛选后的值

    DataTable dt = (DataTable)gridView1.GridControl.DataSource; DataRow[] drr = dt.Select(gridView1.RowF ...

  10. Java_jsp.jstl.Function函数标签库.记录

    JSTL Functions标签库 本书第12章的12.3节(定义和使用EL函数)介绍了EL函数的创建和使用方法.在JSTL Functions标签库中提供了一组常用的EL函数,主要用于处理字符串,在 ...