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();
    }
};
如此簡潔,自己還得繼續加油!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章