671. Second Minimum Node In a Binary Tree
来源:互联网 发布:京东图书淘宝 编辑:程序博客网 时间:2024/06/06 07:19
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
- 深入浅出之正则表达式(一)
- 深入浅出之正则表达式(二)
- select元素javascript常用操作
- 3p0连接mysql经常断开连接解决方案
- 【Scikit-Learn 中文文档】数据集加载工具
- 671. Second Minimum Node In a Binary Tree
- text_area限定字数
- sql_oracle_实用命令
- html进度条
- C++ 压缩文件与解压文件
- 防火墙添加端口
- 批量删除90天前日期命名的文件夹.bat
- 信息系统集成及服务项目管理员延续登记继续教育推荐课程2问答
- 根据内容查存储过程