原题链接在这里: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. PAT 天梯赛 L1-028. 判断素数 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...

  2. 大型网站系统与 Java 中间件实践

    http://wanglizhi.github.io/2016/07/27/JavaWeb-And-MiddleWare/ 第一章 分布式系统介绍 分布式系统的定义:组件分布在网络计算机上,组件间仅仅 ...

  3. 建议47:使用logging记录日志信息

    # -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...

  4. Python的operator.itemgetter函数和sorted函数

    写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...

  5. 斯坦福机器学习视频笔记 Week7 支持向量机 Support Vector Machines

    SVM被许多人认为是最强大的“黑箱”学习算法,并通过提出一个巧妙选择的优化目标,今天最广泛使用的学习算法之一. Optimization Objective 根据Logistic Regression ...

  6. Java 访问修饰符总结

    Java中的访问修饰符 Java面向对象的基本思想之一是封装细节并且公开接口. Java语言采用访问控制修饰符来封装类及类的方法和属性的访问权限,从而向使用者暴露接口.隐藏细节. Java访问控制分为 ...

  7. springmvc请求参数的绑定和获取

    请求参数的绑定和获取: 获取页面请求的参数,是javaweb必不可少的一个环节,在struts中,是通过再Action中定义属性,或者Model的方式进行数据绑定和获取.需要提供setter或gett ...

  8. Java中的UDP协议编程

    一. UDP协议定义   UDP协议的全称是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层.UDP有不提供数据报分组.组装和不能对数据包 ...

  9. MySQL实时性能监控工具doDBA tools

    doDBA tools是什么? doDBA tools是一个基于控制台的远程监控工具,它不需要在本地/远程系统上安装任何软件,它可以实时收集操作系统.MySQL.InnoDB的实时性能状态数据,并可以 ...

  10. gzip: stdin: unexpected end of file tar: Unexpected EOF in archive

    1.问题描述: 今天解压tar包遇到这样一个问题 使用命令:tar -zxvf  xxxxx.tar.gz gzip: stdin: unexpected end of filetar: Unexpe ...