[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 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. More formally, the property root.val = min(root.left.val, root.right.val) always holds.

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: root = [2,2,5,null,null,5,7]
Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

Input: root = [2,2,2]
Output: -1
Explanation: The smallest value is 2, but there isn't any second smallest value.

Constraints:

  • The number of nodes in the tree is in the range [1, 25].
  • 1 <= Node.val <= 231 - 1
  • root.val == min(root.left.val, root.right.val) for each internal node of the tree.

二叉树中第二小的节点。

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。

更正式地说,root.val = min(root.left.val, root.right.val) 总成立。

给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题很像230题但是这道题不是BST而是一棵普通的二叉树。既然题目说了每个父节点的值一定小于等于他的子节点,然后找的又是第二小的节点,所以可以肯定的是这棵树的根节点 root.val 一定是最小的。可以用先序遍历的思路遍历这棵树,去看左孩子,右孩子和父节点的大小关系。我们创建两个变量 first 和 second 分别记录整棵树里最小的 val 和次小的 val,用 preorder 的方式去遍历整棵树。最后判断 count 变量是否还是 0,因为这里的 count 变量记录的是第二小的元素的个数。如果这个变量一直是 0,说明二叉树里面就没有所谓第二小的值。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     int first = Integer.MAX_VALUE;
12     int second = Integer.MAX_VALUE;
13     int count = 0;
14 
15     public int findSecondMinimumValue(TreeNode root) {
16         helper(root);
17         if (count == 0) {
18             return -1;
19         } else {
20             return second;
21         }
22     }
23 
24     private void helper(TreeNode root) {
25         if (root == null) {
26             return;
27         }
28         // if root.val < the current minimum val
29         if (root.val < first) {
30             second = first;
31             first = root.val;
32         }
33         // first < root.val <= second
34         else if (root.val <= second && root.val > first) {
35             count++;
36             second = root.val;
37         }
38         helper(root.left);
39         helper(root.right);
40     }
41 }

JavaScript实现

 1 /**
 2  * @param {TreeNode} root
 3  * @return {number}
 4  */
 5 var findSecondMinimumValue = function (root) {
 6     let first = Infinity;
 7     let second = Infinity;
 8     let count = 0;
 9     var helper = function (root) {
10         if (root == null) {
11             return;
12         }
13         if (root.val < first) {
14             second = first;
15             first = root.val;
16         } else if (root.val <= second && root.val > first) {
17             count++;
18             second = root.val;
19         }
20         helper(root.left);
21         helper(root.right);
22     };
23 
24     helper(root);
25     if (second == Infinity) {
26         return -1;
27     } else {
28         return second;
29     }
30 };

相关题目

230. Kth Smallest Element in a BST

671. Second Minimum Node In a Binary Tree

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/12529272.html