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

    题目:

    特殊的二叉树,父节点是子节点中较小者,找出二叉树中次小的值。

    ********这个题用java写的…java中是null, 并且数组是‘引用’,不用 - > 用 .

    Input: 
        2
       / 
      2   5
         / 
        5   7
    
    Output: 5
    Explanation: The smallest value is 2, the second smallest value is 5.
    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.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int findSecondMinimumValue(TreeNode root) {
            int []data = new int []{Integer.MAX_VALUE, Integer.MAX_VALUE};
            help(root,data);
            return data[1]!=Integer.MAX_VALUE?data[1]:-1;
        }
        
        public void help(TreeNode root, int []data){//java直接就是引用
            if (root == null)
                return;
            if (root.val<data[0]){
                data[1] = data[0];
                data[0] = root.val;
            }
            if (root.val<data[1] && root.val>data[0])
                data[1] = root.val;
            
            help(root.left,data);
            help(root.right,data);
        }
    }
  • 相关阅读:
    python 安装预编译库注意事项-pip
    Lucene 入门需要了解的东西
    PHPSTORM 与 Xdebug 配合调试
    Windows 下命令行修改文件夹的控制权限 Cacls
    PHP 解压zip文件的函数封装
    PHP 关于回调的用法
    CentOS7 安装 swoole
    CentOS7 安装 scala 2.11.1
    PHP 代码质量检测工具的安装与使用
    PHP 新建动态类的代码
  • 原文地址:https://www.cnblogs.com/sherry-yang/p/8490437.html
Copyright © 2020-2023  润新知