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);
}
}

最新文章

  1. 深入理解Java:SimpleDateFormat安全的时间格式化
  2. Web实时通信
  3. HDU 3664 Permutation Counting (DP)
  4. poj3265
  5. 修复直接删除linux系统后grub丢失错误
  6. android-配置虚拟机Virtual device
  7. HTML5 文件域+FileReader 读取文件并上传到服务器(三)
  8. C#用SerialPort实现串口通讯
  9. 创建Git 仓库及 克隆、拉取、和推送操作
  10. echarts-map-区县
  11. java与eclipse的工作小结
  12. day 42 mycql 数据类型
  13. ABP框架系列之四十四:(OWIN)
  14. TraceView工具的使用
  15. linux 释放内存及查看内存命令
  16. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
  17. tortoise svn 忽略bin、obj等文件夹
  18. Oracle多表关联如何更新多个字段
  19. 【服务器】如何在服务器发布网站?Sasa讲解
  20. 如何提高PHP执行效率

热门文章

  1. python之路8-内置模块介绍
  2. python中方法的总结
  3. Java 获取当前线程、进程、服务器ip
  4. Django中ORM介绍
  5. 一文入门HTML5
  6. ReactNative开发笔记(持续更新...)
  7. Java动态代理实现及实际应用
  8. Cucumber启动类配置
  9. 关于 redis 的 数据类型 和 内存模型
  10. sklearn中的损失函数