【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 node in this tree has exactly two or 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.
题目:
特殊的二叉树,父节点是子节点中较小者,找出二叉树中次小的值。
********这个题用java写的…java中是null, 并且数组是‘引用’,不用 - > 用 .
Input:
2
/ \
2 5
/ \
5 7 Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.
Input:
2
/ \
2 2 Output: -1
Explanation: The smallest value is 2, but there isn't any second smallest value.
/**
* 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) {
int []data = new int []{Integer.MAX_VALUE, Integer.MAX_VALUE};
help(root,data);
return data[]!=Integer.MAX_VALUE?data[]:-;
} public void help(TreeNode root, int []data){//java直接就是引用
if (root == null)
return;
if (root.val<data[]){
data[] = data[];
data[] = root.val;
}
if (root.val<data[] && root.val>data[])
data[] = root.val; help(root.left,data);
help(root.right,data);
}
}
【easy】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 ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- [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
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...
- 671. Second Minimum Node In a Binary Tree 非递减二叉树中第二小的元素
[抄题]: Given a non-empty special binary tree consisting of nodes with the non-negative value, where e ...
- 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
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
- 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 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
随机推荐
- C#导出Excel表格方法
using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using NPOI.SS.Formula.Functions; using System.Re ...
- Spring 完成自动注入(autowire)
目录 两个测试类 普通方式手动注入 普通方式注入的缺点 自动注入的介绍 配置自动注入的方式 配置全局自动注入 局部单独配置 利用注解实现自动注入 两个测试类 package cn.ganlixin.p ...
- Boolean 类型转换
原文作者: louis 原文链接: http://louiszhai.github.io/2015/12/11/js.boolean/ 语法 众所周知, JavaScript有五个基本的值类型:num ...
- web开发中各种宽高
Gosper 曲线:https://www.cnblogs.com/tgzhu/p/8286616.html
- Iterator和Enumeration的区别
从源码可以看出,Iterator除了能读取集合的数据之外,也能数据进行删除操作:而Enumeration只能读取集合的数据,而不能对数据进行修改. Iterator支持fail-fast机制,而Enu ...
- springboot项目中配置swagger-ui
Git官方地址:https://github.com/SpringForAll/spring-boot-starter-swagger Demo:https://github.com/dyc87112 ...
- python可迭代对象和迭代器和生成器
可迭代对象 刚开始我认为这两者是等同的,但后来发现并不是这样:下面直接抛出结论: )可迭代对象包含迭代器. )如果一个对象拥有__iter__方法,其是可迭代对象:如果一个对象拥有next方法,其是迭 ...
- loj121-动态图连通性
Solution 线段树分治, 然后直接在线段树上dfs, 在进入/回溯的过程中维护并查集的merge/split. 对于split操作, 可以在merge时按秩合并, 然后利用栈记录, split时 ...
- cmd 常用命令
注:绿色的为比较常用的命令 命令 名称 ASSOC 显示或修改文件扩展名关联. ATTRIB 显示或更改文件属性. BREAK 设置或清除扩展式 CTRL+C 检查. CACLS 显示或修改文件的 ...
- mysql 数据库学习笔记一
一.navicat的引入: (第三方可视化的客户端,方便MySQL数据库的管理和维护)安装网址:https://www.navicat.com.cn/ NavicatTM是一套快速.可靠并价格相宜的数 ...