• [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
  • 相关阅读:
    如何利用FineBI做财务分析
    mysqlbinlog 读取多个文件
    Chapter 13. Miscellaneous PerlTk Methods PerlTk 方法杂项:
    跨越多台haproxy 跳转
    haproxy redirect prefix
    大数据决策领跑零售业
    大数据决策领跑零售业
    perl 实现微信简版
    perl 调用按钮输出到文本框
    Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件
  • 原文地址:https://www.cnblogs.com/immjc/p/7509128.html
Copyright © 2020-2023  润新知