671. Second Minimum Node In a Binary Tree
来源:互联网 发布:数据采集解决方案 编辑:程序博客网 时间:2024/06/05 04:46
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.
Example 1:
Input: 2 / \ 2 5 / \ 5 7Output: 5Explanation: The smallest value is 2, the second smallest value is 5.
Example 2:
Input: 2 / \ 2 2Output: -1Explanation: 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[1] != Integer.MAX_VALUE ? data[1] : -1; } public void help(TreeNode root, int[] data) {if (root == null)return;if (root.val < data[0]) {data[1] = data[0];data[0] = root.val;} else if (root.val < data[1] && root.val > data[0])data[1] = root.val;help(root.left, data);help(root.right, data);}}
阅读全文
0 0
- 671. Second Minimum Node In a Binary Tree
- leetcode 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- leetcode 671. Second Minimum Node In a Binary Tree
- leetcode 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- LeetCode 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- 671. Second Minimum Node In a Binary Tree
- Second Minimum Node In a Binary Tree问题及解法
- Leetcode671. Second Minimum Node In a Binary Tree
- [LeetCode] Second Minimum Node In a Binary Tree
- LeetCode#671 Second Minimum Node In a Binary Tree (week13)
- LeetCode671. Second Minimum Node In a Binary Tree
- leetcode 671. Second Minimum Node In a Binary Tree 第二小数字 + 深度优先遍历DFS
- VirtualBox虚拟机中安装CentOS 7(五) 安装JDK
- Android核心服务解析篇(三)——Android系统的启动
- Java反射简单内容整理(学习笔记)
- list遍历里面的坑
- 怎么更改visual studio新建项目的默认路径
- 671. Second Minimum Node In a Binary Tree
- Android线程管理之AsyncTask异步任务
- Java NIO Buffer
- Java多线程--队列同步器
- UNIX环境编程学习笔记(17)——进程管理之进程的几个基本概念
- windows创建一个执行可执行文件的进程!
- ansj分词使用
- 小学生学嵌入式:)Linux_字符设备驱动程序_初探
- Java 代码性能优化总结