class Solution 
{ 
public:    
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) 
    { 
        if (nums.size() == 0)
            return NULL;        
        else if (nums.size() == 1) 
        { 
            TreeNode * node = new TreeNode(nums[0]);            
            return node; 
        } 
        else 
        { 
            int max = 0;            
            for (int i = 0; i<nums.size(); i++) 
            { 
                if (nums[i]>nums[max])                    
                    max = i; 
            }            
            vector<int> left_num, right_num;            
            for (int i = 0; i<max; i++) 
            { 
                left_num.push_back(nums[i]); 
            }            
            for (int j = max + 1; j<nums.size(); j++) 
            { 
                right_num.push_back(nums[j]); 
            }            
            TreeNode * node = new TreeNode(nums[max]);            
            node->left = constructMaximumBinaryTree(left_num);            
            node->right = constructMaximumBinaryTree(right_num);            
            return node; 
        } 
    } 
};

 

posted on 2018-10-07 23:42  Sempron2800+  阅读(98)  评论(0编辑  收藏  举报