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:

Input:
2
/ \
2 5
/ \
5 7 Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

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

题意:

求二叉树次小结点

Solution1:DFS

code

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution { int second = Integer.MAX_VALUE; public int findSecondMinimumValue(TreeNode root) {
if(root==null) return -1;
helper(root, root.val);
return second == Integer.MAX_VALUE ? -1 : second;
} public void helper(TreeNode node, int rootVal){
if(node == null){return;} if(node.val > rootVal && second > node.val){
second = node.val;
} helper(node.left, rootVal);
helper(node.right, rootVal);
}
}

[leetcode]277. Find the Celebrity谁是名人的更多相关文章

  1. [LeetCode] 277. Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  2. [leetcode]277. Find the Celebrity 找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  3. 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  4. LeetCode 277. Find the Celebrity (找到明星)$

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  5. [LeetCode#277] Find the Celebrity

    Problem: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  6. [LeetCode] Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  7. 【LeetCode】277. Find the Celebrity 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  8. 277. Find the Celebrity

    题目: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exi ...

  9. [LC] 277. Find the Celebrity

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

随机推荐

  1. 第3章 文件I/O(7)_高级文件操作:存储映射

    8. 高级文件操作:存储映射 (1)概念: 存储映射是一个磁盘文件与存储空间的一个缓存相映射,对缓存数据的读写就相应的完成了文件的读写. (2)mmap和munmap函数 头文件 #include&l ...

  2. spring-mvc注解配置小记

    Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...

  3. 基于Linux的Samba开源共享解决方案测试(五)

    对于客户端的网络监控如图: 双NAS网关50Mb码率视音频文件的稳定写测试结果如下: 100Mb/s负载性能记录 NAS网关资源占用 稳定写 稳定写 CPU空闲 内存空闲 网卡占用 NAS1 16个稳 ...

  4. 2.1_Scikit-learn数据集

    scikit-learn数据集 我们将介绍sklearn中的数据集类,模块包括用于加载数据集的实用程序,包括加载和获取流行参考数据集的方法.它还具有一些人工数据生成器. sklearn.dataset ...

  5. uva-167-枚举

    题意:八皇后问题,要求选取的总和最大 #include<stdio.h> #include<iostream> #include<sstream> #include ...

  6. 3.circle (圆)

    1.HTML代码: <div class="circleOne"></div> <br/> <div class="circle ...

  7. 39. 在linux下装好Tomcat要给 tomcat/bin/下面所有.sh的文件执行权限

    chmod a+x *.sh(赋予可执行的权限)

  8. Redis 集合 set 操作, 有序集合

    01, 唯一性, 确定性, 无序性 ( 结合的三大特性 ) 02, 新建集合, 或者往集合中添加数据 => sadd key value1 value2 value3 ....... 03, 查 ...

  9. NPOI操作word文档1

    1.首先进行XWPFDocument类的实例化,该类的实例对应一个word文档 XWPFDocument MyDoc = new XWPFDocument(); 2.设置页面的大小 CT_SectPr ...

  10. jQuery上传插件uploadify 3.2.1

    1.属性 名称 默认值 说明 swf   [必须设置]swf的路径 uploader   [必须设置]服务器端脚本文件路径 uploadLimit 999 最多上传文件数量 checkExisting ...