LeetCode Second Minimum Node In a Binary Tree
原题链接在这里: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的更多相关文章
- [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 ...
- LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
- 【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 ...
- 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 ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)
这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...
- [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 ...
- 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 ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
随机推荐
- range精讲
var ec = range.endContainer endContainer不是一个引用类型 range是引用类型 range经过改变范围之后 var ec2 =range.endContaine ...
- str字符串、bool类型常用方法总结
字符串拼接 必须是字符串与字符串拼接 print('马化腾'+'马云') print('马化腾' * 10) 将打印10个马化腾 字符串翻转 [ : :-1] 字符串可以加和乘,不能减和乘 input ...
- VC6.0 开发 64 位程序
1. 设置平台SDK(如:Microsoft platform sdk 2003),选择64位的编译.链接环境. setenv /XP64 /DEBUG 2. 利用这个环境启动VC6.0. msdev ...
- Linux文件系统管理 挂载命令mount
概述 mount命令用来挂载Linux系统外的文件. Linux 中所有的存储设备都必须挂载之后才能使用,包括硬盘.U 盘和光盘(swap 分区是系统直接调用的,所以不需要挂载).不过,硬盘分区在安装 ...
- Linux yum源码包安装和卸载
Linux 下的绝大多数源码包都是用 C 语言编写的,还有少部分是用 C++ 等其他程序语言编写的.所以,要想安装源码包,必须安装 C 语言编译器 gcc(如果是用 C++ 编写的程序,则还需要安装 ...
- 【Head First Servlets and JSP】笔记11:cookie
容器如何知道客户是谁?(这并不是HTTP能实现的!IP地址不能唯一的标识用户,另外,非必要不采用HTTPS 继续mark孤傲苍狼的博客,百科全书 cookie——Header——字典——键值对—— 延 ...
- 测试连接oracle数据库耗时
maven项目 主程序:ConnOracle.java package org.guangsoft.oracle; import java.sql.Connection; import java.sq ...
- jquery图片过滤归类应用
在线演示 本地下载
- Android SDK组件:webview笔记
1.安卓手机中内置了一款webkit内核的浏览器,在SDK中封装为WebView组件. 2.该组件可以在自己的应用程序中显示本地或者Internet上的网页,也可以把它当作一个浏览器来时用. 3.We ...
- 把已安装的wampserver移动到不同目录使用应注意的问题
很多时候需要把已安装的wampserver移动到不同目录使用,此时应注意几个问题: 1.修改D:\wamp64\bin\apache\apache2.4.9\conf目录下的httpd.conf文件( ...