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.
  8.  
  9. 题意:给定一颗二叉树,返回二叉树中第二最小的结点值,如果不存在第二最小值,则返回-1。对于给定二叉树的规定:

1. 二叉树非空,且二叉树中的结点值非负;
 2. 二叉树中每个结点要么有两个子结点,要么没有子结点;
 如果某父结点有两个子结点,则父结点是两个子结点中的较小值,其中两个子结点值可以相等。

思路:根结点一定是最小值。采用遍历的方法逐个判断是否为第二小元素,实现如下:

  1. public int findSecondMinimumValue(TreeNode root) {
  2. int sMin = root.val, min = root.val;
  3. Queue<TreeNode> q = new LinkedList<>();
  4. q.offer(root);
  5. while(!q.isEmpty()){
  6. TreeNode t = q.poll();
  7. if((sMin == min || t.val < sMin) && t.val > min)//sMin == min来获取第一个比root.val大的元素
  8. sMin = t.val;
  9. if(t.left != null)
  10. q.offer(t.left);
  11. if(t.right != null)
  12. q.offer(t.right);
  13. }
  14. return sMin == min ? -1 : sMin;
  15. }

LeetCode提供的算法1:

先使用深度优先搜索的方法遍历二叉树,并利用set不包含重复元素的特性,将树中的结点存入set对象;然后再找第二小的元素。

  1. public void dfs(TreeNode root, Set<Integer> uniques){
  2. if(root != null){
  3. uniques.add(root.val);
  4. dfs(root.left, uniques);
  5. dfs(root.right, uniques);
  6. }
  7. }
  8. public int findSecondMinimumValue(TreeNode root){
  9. Set<Integer> uniques = new HashSet<>();
  10. dfs(root, uniques);
  11. int min = root.val;
  12. long second = Long.MAX_VALUE;
  13. for(int val : uniques){
  14. if(val > min && val < second)
  15. second = val;
  16. }
  17. return (int) (second == Long.MAX_VALUE ? -1 : second);
  18. }

算法2:对算法1进行改进,因为只需要找第二小的值,因此不需要存储树中所有不重复出现的元素。当遍历某个结点node时,如果node.val > min,则说明该结点node的子树的所有值都大于等于node.val,因此结点node的子树中就不用再判断了。

  1.    int min;
  2. long ans = Long.MAX_VALUE;
  3. public void dfs(TreeNode root){
  4. if(root != null){
  5. if(root.val > min && root.val < ans)
  6. ans = root.val;
  7. else if(root.val == min){
  8. dfs(root.left);
  9. dfs(root.right);
  10. }
  11. }
  12. }
  13. public int findSecondMinimumValue(TreeNode root){
  14. min = root.val;
  15. dfs(root);
  16. return ans < Long.MAX_VALUE ? (int) ans : -1;
  17. }

LeetCode 671. Second Minimum Node In a Binary Tree的更多相关文章

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

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

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

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

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

  5. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

  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. Maven学习(十八)-----Maven依赖管理

    其中一个Maven的核心特征是依赖管理.管理依赖关系变得困难的任务一旦我们处理多模块项目(包含数百个模块/子项目). Maven提供了一个高程度的控制来管理这样的场景. 传递依赖发现 这是很通常情况下 ...

  2. ffmpeg 压缩H265 Windows 硬件编码

    硬件NVIDIA:ffmpeg.exe -i input.avi -c:v hevc_nvenc -preset:v fast output.mp4 软件          :ffmpeg.exe - ...

  3. Jmeter接口测试(九)授权

    下面应该是jmeter的授权设置,但是由于本人目前对这块了解还不深,暂时写个标题,以后有时间再来补充,大家可以先看下一篇内容

  4. Python单元测试--unittest(一)

    unittest模块是Python中自带的一个单元测试模块,我们可以用来做代码级的单元测试. 在unittest模块中,我们主要用到的有四个子模块,他们分别是: 1)TestCase:用来写编写逐条的 ...

  5. Linux 安装Zookeeper<单机版>(使用Mac远程访问)

    阅读本文需要先阅读安装Zookeeper<准备> 新建目录 mkdir /usr/local/zookeeper 解压 cd zookeeper压缩包所在目录 tar -xvf zooke ...

  6. JavaFX 学习笔记——jfoenix类库学习——raised风格按钮创建

    创建按钮 JFXButton jfxb = new JFXButton("hello"); jfxb.getStyleClass().add("button-raised ...

  7. 二叉树的宽度<java版>

    二叉树的宽度 思路:层序遍历的时候,记录每层的节点数量,最后取记录中的最多的数量. 代码实现: public int solution(TreeNode node){ LinkedList<Tr ...

  8. 关于SQL 语句常用的一些查询收藏

    create database xuesheng go use xuesheng go /*学生表*/ create table Student ( S# ,) primary key, Sname ...

  9. Action Required: Listings Deactivated for Potential Pricing Error

    Dear Seller, We are contacting you because we have detected potential pricing errors in your Amazon. ...

  10. JS数据结构学习之排序

    在看<>这本书中关于排序这一章的时候,我试着用javascript语言来重写里面几个经典的排序方法,包括冒泡排序.快速排序.选择排序.插入排序还有希尔排序. 一.冒泡排序 冒泡排序算是排序 ...