654. Maximum Binary Tree(最大二叉树)

Description:

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

题目意思是根据vector里元素大小构造一棵最大二叉树,下面依次列出我的(很low的)解决方案以及大佬的(nb的)解决方案。

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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)
    {
        int pos=0,val=0;
        vector<int> leftNums,rightNums;

        findMax(nums,pos,val);
        TreeNode *root = new TreeNode(val);
        partition2(nums,pos,leftNums,rightNums);
        if(!leftNums.empty())
        {
            TreeNode *left = constructMaximumBinaryTree(leftNums);
            root->left=left;
        }
        if(!rightNums.empty())
        {
            TreeNode *right = constructMaximumBinaryTree(rightNums);
            root->right=right;
        }
        return root;
    }
    void findMax(vector<int>& nums,int& pos,int& val)
    {
        vector<int>::iterator maxPosition = max_element(nums.begin(),nums.end());    //auto replace vector<int>::iterator
        pos=maxPosition-nums.begin();
        val=*maxPosition;
    }
    void partition2(vector<int>& nums,int& pos,vector<int>& leftNums,vector<int>& rightNums)
    {
        for(int i=0; i<(int)nums.size(); i++)
        {
            if(i<pos)
            {
                leftNums.push_back(nums[i]);
            }
            else if(i>pos)
                rightNums.push_back(nums[i]);
        }
    }
};
void outPut(TreeNode* root)
{
    if(root!=NULL)
    {
        cout<<root->val<<" ";
        if(root->left!=NULL) outPut(root->left);
        if(root->right!=NULL) outPut(root->right);
    }
}
int main()
{
    int arr[]= {3,2,1,6,0,5};
    vector<int> ary(arr,arr+6);
    Solution s;
    TreeNode* root=NULL;

    root=s.constructMaximumBinaryTree(ary);
    outPut(root);

    return 0;
}

然后看看大佬的solution (不用递归):

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        vector<TreeNode *> v;
        for(const auto &num:nums) {
            TreeNode *cur=new TreeNode(num);
            while(!v.empty()&&v.back()->val<num) {
                cur->left=v.back();
                v.pop_back();
            }
            if(!v.empty()) v.back()->right=cur;
            v.push_back(cur);
        }
        return v.front();
    }
};
如此简洁,自己还得继续加油!


猜你喜欢

转载自blog.csdn.net/did_you/article/details/79302081