zoukankan      html  css  js  c++  java
  • [LeetCode] Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

    1. The root is the maximum number in the array.
    2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
    3. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.

    Construct the maximum tree by the given array and output the root node of this tree.

    Example 1:

    Input: [3,2,1,6,0,5]
    Output: return the tree root node representing the following tree:
    
          6
        /   
       3     5
            / 
         2  0   
           
            1

    Note:

    1. The size of the given array will be in the range [1,1000].

    给定一个元素不重复的数组,构建一个由该数组组成的最大树,这个最大树要求如下:

    1、根节点是数组中的最大数

    2、左子树的根节点由这个最大数分割数组的左边部分的最大数组成。

    3、右子树的根节点由这个最大数分割数组的右边部分的最大数组成。

    构建这个树并返回这个树的根节点。

    方法1:使用迭代的方法。

    使用一个vector来模拟栈存储数组节点。

    如果当前数组为空,则将当前节点直接入栈

    如果当前数组不为空,比较当前节点curNode与nodeVec->back()的val的大小。

    1、如果当前数组非空并且当前节点值大于数组尾元素。则说明数组尾元素是当前节点的左子节点。此时弹出数组中元素。

    2、如果当前元素非空,则说明当前节点为数组尾元素的右子节点。

    将当前节点放入数组中。

    最后返回数组中的头元素。

    /**
     * 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* constructMaximumBinaryTree(vector<int>& nums) {
            vector<TreeNode*> nodeVec;
            for (int i = 0; i < nums.size(); i++) {
                TreeNode* curNode = new TreeNode(nums[i]);
                if (nodeVec.empty()) {
                    nodeVec.push_back(curNode);
                }
                else {
                    while (!nodeVec.empty() && nodeVec.back()->val < curNode->val) {
                        curNode->left = nodeVec.back();
                        nodeVec.pop_back();
                    }
                    if (!nodeVec.empty()) {
                        nodeVec.back()->right = curNode;
                    }
                    nodeVec.push_back(curNode);
                }
            }
            return nodeVec.front();
        }
    };
    // 65 ms

     方法2:使用递归的方法

    对于每一步

    1、找出最大元素

    2、根据最大元素将数组分为两部分

    3、将最大元素的左右节点迭代分配

    返回根节点

    /**
     * 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* constructMaximumBinaryTree(vector<int>& nums) {
            if (nums.empty())
                return nullptr;
            
            // use auto to declare iterator
            auto it = max_element(nums.begin(), nums.end());
            int maxVal = *it;
            
            // use auto to init with new; use nullptr instead of NULL
            auto* node = new TreeNode(maxVal);
            vector<int> left_nums(nums.begin(), it);
            vector<int> right_nums(it + 1, nums.end());
            node->left = constructMaximumBinaryTree(left_nums);
            node->right = constructMaximumBinaryTree(right_nums);
            
            return node;
        }
    };
    // 74 ms
  • 相关阅读:
    林正英电影之我见
    雕虫小技,颇感羞愧。
    简单地求最大公约数
    大声喊:我现在不喜欢编程!
    简单递归题,核反应堆中有α和β两种粒子...
    递归简单题2
    Java接口和抽象类的理解
    如何入门计算机高级程序语言,进化菜鸟程序员
    memcached安装报错 error while loading shared libraries: libevent2.0.so.5: cannot open shared object file: No such file or directory解决
    linux mount 过程
  • 原文地址:https://www.cnblogs.com/immjc/p/8287358.html
Copyright © 2011-2022 走看看