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[]!=Integer.MAX_VALUE?data[]:-;
} public void help(TreeNode root, int []data){//java直接就是引用
if (root == null)
return;
if (root.val<data[]){
data[] = data[];
data[] = root.val;
}
if (root.val<data[] && root.val>data[])
data[] = root.val; help(root.left,data);
help(root.right,data);
}
}

【easy】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. [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 ...

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

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

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

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

  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. NodeJs操作MongoDB之多表查询($lookup)与常见问题

    NodeJs操作MongoDB之多表查询($lookup)与常见问题 一,方法介绍 aggregate()方法来对数据进行聚合操作.aggregate()方法的语法如下 1 aggregate(ope ...

  2. Module build failed: Error: Cannot find module 'babel-runtime/core-js/get-it

    npm i babel-loader@7.1.5 -D

  3. Linux学习之路1

    root用户 安装初始系统默认没有启动root用户,如下方式启动: l  sudo passwd l  连续输入两次密码,启动root用户 l  再登陆 登陆root用户 l  su root l  ...

  4. Capability配置简介

    什么是Capability desired capability的功能是配置Appium会话.他们告诉Appium服务器您想要自动化的平台和应用程序. Desired Capabilities是一组设 ...

  5. bugku 逆向 love

    可以看到将输入先经过 sub_4110BE 这个函数进行加密 然后每一位加上下标本身 再和str2比较 正确就是right 点开加密函数: 关键语句就在这里 我们可以看到是算输入的字符按三个一组能分为 ...

  6. ZooKeeper连接并创建节点以及实现分布式锁操作节点排序输出最小节点Demo

    class LockThread implements Runnable { private DistributedLock lock; public LockThread(int threadId, ...

  7. Linux lvs三种模式工作原理

    LVS:Linux Virtual Sevser,Linux虚拟服务器,由章文嵩先生成立的有关负载均衡的开源项目.基于Linux内核.优化后并发量可以达到百万级. NAT模式: NAT(Network ...

  8. Ansible 插件 操作介绍

    一.Ansible 插件 之 [统计任务处理时间] 在做性能优化之前首先需要做的是收集一些统计数据,这样才能为后面做的性能优化提供数据支持,对比优化前后的结果.非常不错的是,在 github 发现一个 ...

  9. java中getAttribute与getParameter方法的区别

    知识点1:getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为object对象类型 例: r ...

  10. Java基础--面向对象编程1(类与对象)

    1.类(class)的定义 类是对一组具有相同特征和行为的对象的抽象描述. 在程序中,引入类的概念,就是为了快速生成更多的具有相同特性和行为的事物. 2.对象(object)的定义 对象是类的具体实现 ...