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. mono部分源码解析

    一.源码结构 这里仅列举几个重要的目录:mcs:    mcs: Mono实现的基于Ecma标准的C#编译器.    class: CLI的C#级的实现.类似于Android中的Java层,应用程序看 ...

  2. java内存模型(二)深入理解java内存模型的系列好文

    深入理解java内存模型(一)--基础 深入理解java内存模型(二)--重排序 深入理解java内存模型(三)--顺序一致性 深入理解java内存模型(四)--volatile 深入理解java内存 ...

  3. mybatis匹配字符串的坑

    where语句中我们经常会做一些字符串的判断,当传入的字符串参数为纯数字时,在mybatis的条件语句test里匹配全数字字符串需要注意会有如下现象: 所以里面的字符串需要加单引号,mybatis是匹 ...

  4. 配置Ubuntu虚拟环境

    1.ubuntu默认root用户没有激活,激活root用户,就要为root用户创建密码 $sudo passwd root   2.修改主机名 $vi /etc/hostname   3.安装ssh服 ...

  5. 使用html和CSS进行网页网站设计 -- 简明步骤

    网页制作流程: 1. 心中有规划,网站的骨架结构,页面布局layout. 2. 创建一个用于创建模板dwt的html页: main.html 3. 制作main.html: (1) 在html文件中依 ...

  6. 返回标签数据示例 (PHP)

    标签接口函数 获取标签数据 array uc_tag_get(string tagname [, array nums]) 函数参数 参数 含义 string tagname 标签名称 array n ...

  7. restfull 风格 参考 https://blog.csdn.net/jaryle/article/details/52141097

    https://www.cnblogs.com/xiaoxian1369/p/4332390.html :

  8. leetcode83

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  9. leetcode541

    public class Solution { public string ReverseStr(string s, int k) { var len = s.Length; //记录k的倍数 //分 ...

  10. 高性能JSON框架之FastJson的简单使用

    1.前言 1.1.FastJson的介绍: JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的 ...