原题链接在这里:https://leetcode.com/problems/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.

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.

题解:

If current root or root.left == null, then return -1.

Otherwise, check root.left.val and root.right.val, if they != root.val, it must be larger than root.val, could be candidate.

Otherwise, continue dfs on the side == root.val.

If both sides != -1, return min, otherwise, return max.

Time Complexity: O(n). Space: O(logn).

AC Java:

 /**
* 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) {
if(root == null || root.left == null){
return -1;
} int l = root.left.val;
int r = root.right.val;
if(l == root.val){
l = findSecondMinimumValue(root.left);
} if(r == root.val){
r = findSecondMinimumValue(root.right);
} if(l > 0 && r > 0){
return Math.min(l, r);
}else if(l > 0){
return l;
}else if(r > 0){
return r;
}else{
return -1;
}
}
}

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

  1. [LeetCode] 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 ...

  2. LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

    671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...

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

  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. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

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

  6. LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)

    这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...

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

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

  9. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

随机推荐

  1. iOS WKWebView OC 与 JS 交互学习

    我写WKWebView 想让 服务端相应 一个 方法但是不响应,根据 UIWebView 用 JSContext就能拿到响应的处理经验是不是服务端 也需要 对 WKwebView有兼容的一个写法??? ...

  2. iOS 关于 Missing iOS Distribution signing identity for.... 等 打包 校验 出现的事故 处理经验

    着实郁闷了一阵子,不知道为什么 证书和配置文件都没有问题 在Archieve后  validate 提示:"Missing iOS Distribution signing identity ...

  3. 【转载】格式化存储装置成为 Ext2/Ext3/Ext4 档案系统

    格式化 用系统管理员帐户 (即 root) 身份打「mkfs -t ext2|ext3|ext4 储存装置」: mkfs -t ext3 /dev/sdb5 要格式化档案系统为 Ext2,亦可以直接使 ...

  4. tomcat添加登录用户名密码

    tomcat版本 apache-tomcat-7.0.55.tar.gz 编辑 TOMCAT_HOME/conf/tomcat-users.xml在tomcat-users里面添加 <tomca ...

  5. qq在线客服代码

    http://wpa.qq.com/msgrd?v=3&uin=1456262869&site=www.cactussoft.cn&menu=yes

  6. Elasticsearch-->Get Started-->Basic concepts

    https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-concepts.html There ...

  7. 【P2052】道路修建(树形+搜索)

    这个题看上去高大上,实际上就是一个大水题.怎么说呢,这个题思路可能比较难搞,代码实现难度几乎为0. 首先我们可以发现这是一棵树,然后问其中任意一条边左右两边的点的数量之差的绝对值,实际上,无论两边的点 ...

  8. json数据的拼接与解析

    json数据格式 [{ "firstName": "Brett", "lastName":"McLaughlin", & ...

  9. 泛型学习第一天:List与IList的区别 (一)

    先看代码: using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace L ...

  10. 手把手教你安装SSL证书升级https

    是不是觉得别人网站前面的小绿锁很好看? 而且,Google官方也正式承认过https是影响搜索排名的一个因素,那么如何将自己的网站全面升级为https呢?今天的内容就介绍一下如何将部署在Nginx的W ...