zoukankan      html  css  js  c++  java
  • [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.

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

    Example 2:

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

    求二叉树中的第二小的节点:首先遍历二叉树后排序。然后在数组中找出第二小的元素即可。如果不存在第二小的元素,则返回-1

    /**
     * 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:
        vector<int> res;
        int findSecondMinimumValue(TreeNode* root) {
            inorder(root);
            sort(res.begin(), res.end());
            int smaller = -1;
            for (int i = 0; i != res.size() - 1; i++) {
                if (res[i + 1] - res[i] > 0) {
                    smaller = res[i+1];
                    break;
                }
            }
            return smaller;
        }
        vector<int> inorder(TreeNode* root) {
            if (root == nullptr)
                return res;
            inorder(root->left);
            res.push_back(root->val);
            inorder(root->right);
            return res;
        }
    };
    // 3 ms

     根据题意,每个节点都有0个或者2个子节点,并且该节点的值是两个子节点中值较小的那个。说明如果以每组父节点和两个子节点中,一定有2个节点的数值相同,且为父节点与其中一个子节点数值相同。

    遍历二叉树。可知根节点一定是最小的那个节点。要找出次小节点,则需要继续遍历。如果根节点的某一子节点与根节点相同,则另一个子节点的值就是次小值。

    另外一种情况是所有节点值相同,需要递归遍历所有节点,返回-1。

    /**
     * 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:
        int findSecondMinimumValue(TreeNode* root) {
            if (root == nullptr)
                return -1;
            return minVal(root, root->val);
        }
        int minVal(TreeNode* root, int first) {
            if (root == nullptr)
                return -1;
            if (root->val != first)
                return root->val;
            int left = minVal(root->left, first), right = minVal(root->right, first);
            if (left == -1)
                return right;
            if (right == -1)
                return left;
            return min(left, right);
        } 
    };
    // 3 ms

    还可以使用set进行去重,利用层次遍历将数中的每个节点值插入set中。利用set的性质,返回第二个set值即可。

    /**
     * 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:
        int findSecondMinimumValue(TreeNode* root) {
            if (root == nullptr)
                return -1;
            set<int> s;
            queue<TreeNode*> q;
            q.push(root);
            while (!q.empty()) {
                TreeNode* node = q.front();
                q.pop();
                s.insert(node->val);
                if (node->left != nullptr)
                    q.push(node->left);
                if (node->right != nullptr)
                    q.push(node->right);
            }
            s.erase(s.begin());
            auto it = s.begin();
            if (it == s.end())
                return -1;
            return *it;
        }
    };
    // 3 ms
  • 相关阅读:
    Lesson 3 Nehe
    Lesson 2 Nehe
    Lesson 1 Nehe
    Lesson 1 Nehe
    JavaScript 字符串与数组转换函数[不用split与join]
    华中科大校长:教授被称为“叫兽”是教育的悲哀
    /etc/profile、~/.bash_profile等几个文件的执行过程
    cygwin下遇到system没有执行的问题
    发短信 汉字编码 utf-8 UCS-2BE
    UTF-8与UNICODE的关系及代码转换
  • 原文地址:https://www.cnblogs.com/immjc/p/7509128.html
Copyright © 2011-2022 走看看