原题链接在这里: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. 前端之 Ajax(补)

    概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...

  2. PAT 天梯赛 L1-027. 出租 【模拟】

    题目链接 https://www.patest.cn/contests/gplt/L1-027 题意 给出一串电话号码,找出其中不同数字的个数,并且按递减顺序排列,然后 有一个index 数组,指出每 ...

  3. $用python实现快速排序算法

    本文主要介绍用python实现基本的快速排序算法,体会一下python的快排代码可以写得多么简洁. 1. 三言两语概括算法核心思想 先从待排序的数组中找出一个数作为基准数(取第一个数即可),然后将原来 ...

  4. java基础—— Collections.sort的两种用法

    package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...

  5. 生产环境中,通过域名映射ip切换工具SwitchHosts

    项目中,经常需要配置host.将某个域名指向某个ip.手动配置C:\Windows\System32\drivers\etc\hosts,非常不方便.这里分享一个可以高效切换host工具:Switch ...

  6. SQL中的5种常用的聚集函数

    首先你要知道 where->group by->having->order by/limit  ,这个就是写sql语句时的顺序  常用的5个聚集函数: Max             ...

  7. Secret Code

    Secret Code 一.题目 [NOIP模拟赛A10]Secret Code 时间限制: 1 Sec  内存限制: 128 MB 提交: 10  解决: 6 [提交][状态][讨论版] 题目描述 ...

  8. wpf数据绑定的论述

    (1)绑定模式: <Lable x:Name=lab Content={binding UserName} /> <!--binding相当于SetBinding--> Con ...

  9. Node.js核心模块_全局变量、util学习

    全局对象 javascript的全局对象是window,他及其所有属性都可以在程序的任何地方访问.即全局变量. 而在node中全局对象是global,所有全局变量都是global对象的属性,包括其本身 ...

  10. CC++中sizeof函数的用法

    C/C++中sizeof()函数的用法 学习C/C++有时会遇到下面的情况: 已知 char *str1="absde"; char str2[]="absde" ...