LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点
671. Second Minimum Node In a Binary Tree
题目描述
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1。
每日一算法2019/5/12Day 9LeetCode671. Second Minimum Node In a Binary Tree
示例 1:
```
2
/ \
2 5
/ \
5 7
```
输出: 5
说明: 最小的值是 2,第二小的值是 5。
示例 2:
```
2
/ \
2 2
```
输出: -1
说明: 最小的值是 2,但是不存在第二小的值。
Java 实现
Recursive Solution
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public int findSecondMinimumValue(TreeNode root) {
if (root == null) {
return -1;
}
if (root.left == null && root.right == null) {
return -1;
}
int left = root.left.val;
int right = root.right.val;
if (left == root.val) {
left = findSecondMinimumValue(root.left);
}
if (right == root.val) {
right = findSecondMinimumValue(root.right);
}
if (left != -1 && right != -1) {
return Math.min(left, right);
} else if (left != -1) {
return left;
} else {
return right;
}
}
}
相似题目
参考资料
- https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
- https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/
LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9的更多相关文章
- [Swift]LeetCode671. 二叉树中第二小的节点 | 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 ...
- Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)
671. 二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的 ...
- Leetcode 671.二叉树中第二小的节点
二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树 ...
- [LeetCode] 671. 二叉树中第二小的节点 ☆(递归 合并)
描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...
- [LeetCode]671. 二叉树中第二小的节点(递归)
题目 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...
- 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 ...
- LeetCode671. 二叉树中第二小的节点
题目 纯暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 int findSecondMinimumValue(TreeNode* r ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- 【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 ...
随机推荐
- 坑:pytest 运行报错unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin'
右键运行pytest run时报错,原因是pytest版本过高导致的.有时候会遇到在自己本机没问题,拉取服务器代码下来后就出问题了,所以把pytest版本改低就可以,亲测有效,希望对你有帮助 完整报错 ...
- java代码拼写sql
java后台 String zffwdm=map.get("zffwmc")==null?"":map.get("zffwmc"); ...
- linux 命令scp
scp命令网络传输文件 上传文件 scp 文件名 usename@10.233.23.100:Data/ 上传文件夹到服务器 scp -r 文件夹(不带/)usename@10.233.23.100: ...
- Out of range value for column 'huid' at row
遇到一个MySQL小问题 Data truncation: Out of range value for column 'huid' at row 1 在数据库某表中字段 “huid” 为 ...
- 苹果IPhone真机开发调试
需要 在苹果开发网站 加入真机的UDID, 并在Profile中勾选该手机
- java.lang.UnsupportedClassVersionError: com/mysql/cj/jdbc/Driver : Unsupported major.minor version 52.0 (unable to load class [com.mysql.cj.jdbc.Driver])
原因: com/mysql/cj/jdbc/Driver是6.0版本的驱动,兼容JDK8环境,不兼容JDK7环境,在基于jdk7的tomcat中编译运行会出错,在基于jdk8的tomcat中编译运行则 ...
- [原][bigemap][globalmapper]通过bigemap下载全球30米DEM高程数据(手动下载)(下载全球高精度dom卫片、影像、等高线、矢量路网、POI、行政边界)
本文研究了bigemap下载高程数据的方式,但是严重不推荐使用这总手动方式,bigemap这个软件一次只能下载100M以内的高程数据,即使花钱,也不给你提供批量下载dem的方式!也有些其他更好的软件, ...
- Xadmin权限管理
需求分析: 1.判断用户是否登陆,未登陆就不能进入其他页面2.为用户分配不同的权限,用户的操作只能在权限范围之内3.将用户可操作的权限显示在页面山,点击能进入该页面操作 模型表的建立 1.对每个用户建 ...
- ES6深入浅出-4 迭代器与生成器-4.总结
yield的值就是外面调用next得到的值 ES6给的新的语法,如果你给任何一个对象添加一个Symbol.interator的key,同时它的值是一个生成器. 下面选中的就是生成器.生成返回的东西是迭 ...
- CGI = MCC + MNC + LAC + CI
CGI = MCC + MNC + LAC + CI 摘自:http://www.360doc.com/content/19/0801/10/65611272_852334484.shtml CGI是 ...