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
  • 相关阅读:
    jQuery Ajax 方法调用 Asp.Net WebService 的详细例子(原创)
    jQuery 访问WebService 返回复合类型列表
    Vista Media Center 开发之深入浅出 (一) Vista Media Center开发环境的搭建
    安装一个媒体解码器让 Windows Media Player 支持更多媒体格式
    静静期待 Windows 7 的到来
    集成 RealTek 声卡 在 Windows 7 有杂音、爆音的解决方法
    使用jQuery for Asp.Net 我的开发环境配置
    Windows 7 VHD 启动
    建立一个 C#.Net Windows Service 程序
    Windows server 2008 r2 简体中文180天评估版微软官方下载地址
  • 原文地址:https://www.cnblogs.com/immjc/p/7509128.html
Copyright © 2011-2022 走看看