【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[1]!=Integer.MAX_VALUE?data[1]:-1;
    }

    public void help(TreeNode root, int []data){//java直接就是引用
        if (root == null)
            return;
        if (root.val<data[0]){
            data[1] = data[0];
            data[0] = root.val;
        }
        if (root.val<data[1] && root.val>data[0])
            data[1] = root.val;

        help(root.left,data);
        help(root.right,data);
    }
}

原文地址:https://www.cnblogs.com/sherry-yang/p/8490437.html

时间: 2024-08-29 15:16:10

【easy】671. Second Minimum Node In a Binary Tree的相关文章

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 twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G

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.

LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1. 每日一算法 2019/5/12Day 9 LeetCode671. Second Minimum Node In a B

【easy】235. Lowest Common Ancestor of a Binary Search Tree

题意大概是,找两个节点的最低公共祖先 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* lowestCommonAncestor(Tre

[LeetCode] 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.

[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.

【LeetCode】236. Lowest Common Ancestor of a Binary Tree

Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as th

【leetcode】987. Vertical Order Traversal of a Binary Tree

题目如下: Given a binary tree, return the vertical order traversal of its nodes values. For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1)and (X+1, Y-1). Running a vertical line from X = -infinity t

606. Construct String from Binary Tree 【easy】

606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by empty parenthesis pair "()". And you