[抄题]:

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. 基于spring的异常一站式解决方案

    https://segmentfault.com/a/1190000006749441#articleHeader4 https://lrwinx.github.io/2016/04/28/%E5%A ...

  2. 可以随时查找的max栈和max队列——面试

    这是面试中比较常见的题目,max队列也是编程之美里的一道题 对于max的栈,有个比较简单的办法就是,每次入栈前判断栈顶元素与正在入栈的元素哪个大,哪个大就哪个入栈 对于队列,我们知道可以用两个栈来实现 ...

  3. CRITICAL:yum.cli:Config Error: Error accessing file for config file:///etc/yum.conf

    先试试yum install gcc  , 1,下载最新的yum-3.2.28.tar.gz并解压 #wget http://yum.baseurl.org/download/3.2/yum-3.2. ...

  4. Java程序员之JS(一) 入门

    背景:使用了JS做一个 WEB 项目之后,一直有使用JS 的一个功能,突然某一天项目重新规划,开始自己手动写一些原始JS,情况不妙,原来之前一直是用同事搭建好的框架在开发,对 JS 零基础的我一直在 ...

  5. [转]SQLite 经验集

    SQLite 的默认时间 转自:http://www.cnblogs.com/pennant/archive/2011/08/11/2134897.html select strftime('%Y-% ...

  6. Hadoop MapReduce 初步学习总结

    在Hadoop中一个作业被提交后,其后具体的执行流程要经历Map任务的提交中间结果处理,Reduce任务的分配和执行直至完成这些过程,下面就是MapReduce中作业详细的执行流程图(摘自<Ha ...

  7. 多进程并发简单socket

    server---------------------这种简单实现:坏处客户端启动一个一个进程,进程多了机器要蹦#!/usr/bin/env python # encoding: utf-8  # D ...

  8. pycharm多行代码同时注释、去除注释

    pycharm中同时注释多行代码快捷键: 代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消

  9. Yaml学习笔录

    YAML概要 [YAML](http://yaml.org/)是"YAML Ain't a Markup Language"(YAML不是一种置标语言)的递归缩写,早先YAML的意 ...

  10. Tkinter Radiobutton

    Python GUI - Tkinter Radiobutton: 这个小部件实现了多项选择按钮,这是一种方式向用户提供许多可能的选择,让用户选择只是其中之一.   这个小部件实现了多项选择按钮,这是 ...