描述

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。

给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。

示例 1:

输入:
 2
 / \
2 5
   / \
  5 7

输出: 5
说明: 最小的值是 2 ,第二小的值是 5 。
示例 2:

输入:
 2
 / \
2  2

输出: -1
说明: 最小的值是 2, 但是不存在第二小的值。

解析

从左子树和右子树找到一个比node更大的值。如果两个子树分别返回了两个值就取最小的那个。

代码

public int findSecondMinimumValue(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
return -1;
}
int left = -1;
int right = -1;
if (root.left != null) {
left = root.left.val == root.val ? findSecondMinimumValue(root.left) : root.left.val;
}
if (root.right != null) {
right = root.right.val == root.val ? findSecondMinimumValue(root.right) : root.right.val;
} if (left != -1 && right != -1) {
return Math.min(left, right);
} else if (left != -1) {
return left;
} else if (right != -1) {
return right;
} else {
return -1;
}
}
public int findSecondMinimumValue(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
return -1;
}
List<Integer> list = new ArrayList<>();
findSecondMinimumValueH(root, list, root.val, Integer.MAX_VALUE);
if (list.size() <= 0) {
return -1;
}
int min = list.get(0);
for (int li : list) {
if (li < min) {
min = li;
}
}
return min;
} public void findSecondMinimumValueH(TreeNode root, List<Integer> list, int minVal, int moreVal) {
if (root == null) {
return;
}
if (root.left != null) {
int value = root.left.val;
if (value > minVal && value <= moreVal) {
moreVal = value;//找出第一个大于minVal的值,然后不再继续
list.add(moreVal);
} else {
findSecondMinimumValueH(root.left, list, minVal, moreVal);
}
} if (root.right != null) {
int value = root.right.val;
if (value > minVal && value <= moreVal) {
moreVal = value;//找出第一个大于minVal的值,然后不再继续
list.add(moreVal);
} else {
findSecondMinimumValueH(root.right, list, minVal, moreVal);
}
}
}

这个代码和第一段代码有点区别:记录了所有分支中,第一个大于root.val的值,然后找出最小的,既是第二大的。

[LeetCode] 671. 二叉树中第二小的节点 ☆(递归 合并)的更多相关文章

  1. [LeetCode]671. 二叉树中第二小的节点(递归)

    题目 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...

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

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

  3. Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)

    671. 二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的 ...

  4. Leetcode 671.二叉树中第二小的节点

    二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树 ...

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

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

  7. LeetCode671. 二叉树中第二小的节点

    题目 纯暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 int findSecondMinimumValue(TreeNode* r ...

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

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

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

随机推荐

  1. vscode片段

    参考资料 https://blog.csdn.net/maokelong95/article/details/54379046 "狂客注释": { "prefix&quo ...

  2. hugepage设置相关总结

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/shaoyunzhe/article/de ...

  3. 使用idea创建webservice客户端

     new project: Generate Java Code From Wsdl: 导入junit.jar,编写测试类:

  4. 0010.Regular Expression Matching(H)

    jjc . Regular Expression Matching(hard) Given an input string (s) and a pattern (p), implement regul ...

  5. 比较oracle表字段是否一致

    SELECT M.OWNER ,M.TABLE_NAME ,M.COLUMN_ID ,M.COLUMN_NAME ,M.DATA_TYPE ,M.DATA_LENGTH ,N.OWNER ,N.TAB ...

  6. JS获当前网页元素高度offsetHeight

    本文测试的是offsetHeight,获取网页中某元素的高度,单位是像素,获取的类型是整型,可以进行数字运算.如图,网页中的元素本身的高度包括,自身的内容+padding+border,而margin ...

  7. 2019.12.4 Hystix熔断&Feign进行远程调用&Zuul

    0.学习目标 会配置Hystix熔断 会使用Feign进行远程调用 能独立搭建Zuul网关 能编写Zuul的过滤器 1.Hystrix 1.1.简介 Hystrix,英文意思是豪猪,全身是刺,看起来就 ...

  8. C++ 特性之 lambda

    "我扑到书籍上,就像饥饿的人扑在面包上"-- 高尔基 简而言之,Lambda 表达式就是用于创建匿名函数的. 或许,Lambda 表达式算得上是 C++ 11 新增特性中最激动人心 ...

  9. shrio学习笔记

    Thymeleaf扩展坐标 <!--thyemleaf对shrio的扩展坐标--> <dependency> <groupId>com.github.thebora ...

  10. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...