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 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女王算法只是一种思想,没有具体成型的模板
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 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 非递减二叉树中第二小的元素的更多相关文章
- 【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 ...
- 【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 ...
- 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二叉树中第二小的节点 (C++)
题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where eac ...
- Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- 671. Second Minimum Node In a Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- JAVA-Unit05: 视图、序列、索引 、 约束
Unit05: 视图.序列.索引 . 约束 视图 数据库对象之一 视图在SQL语句中体现的角色与表相同, 但它并非一张真实存在的表,它对应的 是一个查询语句的结果集. 创建一个查看10号部门员工信息的 ...
- js正则验证
/判断输入内容是否为空 function IsNull(){ var str = document.getElementById('str').value.trim(); ...
- jq from表单 取值
//获取表单参数 var DataDeal = { formToJson: function (id) { var data=$(id).serialize();//获取值 data = decode ...
- Django前端的文本编辑器,滑动登陆
文本编译器 # 添加文章 url(r'^addarticle/$', views.addarticle), # 利用文本编辑器添加文章 def addarticle(request): ''' 添加文 ...
- 关于Struts2配置文件名修改的问题
突发奇想的想知道Struts2的配置文件名是否可以修改,自己最早使用Struts2的时候,只是单纯的将配置文件命名为“struts.xml”,这是Strut2默认的配置文件名,我一直也没有去修改它的命 ...
- 缓存varnish的管理及配置详解
一 工作原理 在当前主流的Web服务架构体系中,Cache担任着越来越重要的作用.常见的基于浏览器的C/S架构,Web Cache更是节约服务器资源的关键.而最近几年由FreeBSD创始人之一Kamp ...
- 硬盘IOPS与读写速度
IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求 ...
- ubuntu 12.04 配置-1
今天对ubuntu 12.04 系统进行了相关的配置,配置的主要内容有: 1)php + mysql + apache2 web开发环境的搭建: 2)vim的简单保存退出指令: 3)文件和文件夹权限的 ...
- Flask之模板过滤器
3.2 过滤器: 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,这就用到了过滤器. 过滤器的使用方式为:变量名 | 过滤器. 过滤器名写在 ...
- Novell云计算
老牌操作系统NetWare,淡出人们的视野,已记不清是哪一年的事了.不过,它的拥有者-NOVELL公司,却没有像自己曾经红极一时的OS那样被人遗忘.相反,在今天云计算的大潮中,Novell华丽转身,摇 ...