[抄题]:

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly twoor 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.

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

要判断节点值是不是空节点产生的-1

[奇葩corner case]:

光有一个头节点,也无法输出

[思维问题]:

以为要用break:好像总体来说用得并不多,此题中只要判断是否不相等就行了

[一句话思路]:

DC女王算法只是一种思想,没有具体成型的模板

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. traverse的表达式自身就带有递归循环的效果,不用再加while了。用于改变left的值,就必须把left放在左边

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

能用等号就少用break

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

dc的思想其实没有什么普适性

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

230. Kth Smallest Element in a BST 第k小,用二分法

[代码风格] :

/**
* 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) {
//corner case
if (root == null) {
return -1;
}
if (root.left == null && root.right == null) {
return -1;
}
//define left, right first
int left = root.left.val;
int right = root.right.val;
//find next
if (left == root.val) {
left = findSecondMinimumValue(root.left);
}
if (right == root.val) {
right = findSecondMinimumValue(root.right);
}
//compare
if (left != -1 && right != -1) {
return Math.min(left, right);
}else if (left != -1) {
return left;
}else {
return right;
}
}
}

671. Second Minimum Node In a Binary Tree 非递减二叉树中第二小的元素的更多相关文章

  1. 【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 ...

  2. 【easy】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 ...

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

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

  5. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

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

  7. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  8. 671. Second Minimum Node In a Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

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

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

随机推荐

  1. bzoj 2535 && bzoj 2109 [Noi2010]Plane 航空管制——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2535 https://www.lydsy.com/JudgeOnline/problem.p ...

  2. js的delegate回调例子

    暂时没发现有具体的实际用处,先记录下 <!DOCTYPE html> <html> <head lang="en"> <meta char ...

  3. JVM内存管理之垃圾搜集器参数精解

    本文是GC相关的最后一篇,这次LZ只是罗列一下hotspot JVM中垃圾搜集器相关的重点参数,以及各个参数的解释.废话不多说,这就开始. 垃圾搜集器文章传送门 JVM内存管理------JAVA语言 ...

  4. Java Language Changes for Java SE 9

    Java9引入了module模块的概念,是类与接口和数据资源的一种封装,并可以声明与其他模块的依赖关系.这里总结一下Java9带来的新特性. 更简练的try-with-resources语句 fina ...

  5. jquery json string 转换 合并

    Jquery 1.9.1 var BODY = { "recipients": { "values": [] }, "subject": ' ...

  6. 解决win下无法ping通VM虚拟机CentOS系统的方法

    事情描述:公司迁新址,电脑带过去之后,用xshell连接vm的centos系统老是连接失败,然后考虑到公司迁新址这个情况,我首先怀疑是ip的问题,然后在vm中执行ifconfig找到centos的ip ...

  7. 【转】JMeter使用指南

    Abstract 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成测试 ...

  8. 【AR实验室】mulberryAR:并行提取ORB特征

    本文转载请注明出处 —— polobymulberry-博客园 0x00 - 前言 在[AR实验室]mulberryAR : ORBSLAM2+VVSION末尾提及了iPhone5s真机测试结果,其中 ...

  9. 第三章:Hadoop简介及配置Hadoop-1.2.1,hbase-0.94.13集群

    前面给大家讲了怎么安装Hadoop,肯定会有人还是很迷茫,装完以后原来就是这个样子,但是怎么用,下面,先给大家讲下Hadoop简介:大致理解下就OK了 hadoop是一个平台,提供了庞大的存储和并行计 ...

  10. php502故障处理

    一次打开网站,发现502,第一反应肯定是php-fpm没启动,尝试启动还是502. 1.首先查询Nginx日志发现如下连接PHP失败: 2016/07/29 15:56:04 [error] 2376 ...