[LeetCode] C++: Intermediate Problem-Tree 654. The Largest Binary Tree

654. Largest Binary Tree

Medium difficulty 242

Given an integer array with no repeated elements  nums . A maximum binary tree constructed directly recursively from this array is   defined as follows:

  1. The root of the binary tree is nums the largest element in the array  .
  2. The left subtree is  the largest binary tree recursively constructed from the left part of the maximum value in the array  .
  3. The right subtree is  the largest binary tree recursively constructed from the right part of the maximum value in the array  .

Returns nums the largest binary tree  constructed  with the given array  .

 

Example 1:

Input: nums = [3,2,1,6,0,5]
 Output: [6,3,5,null,2,0,null,null,1]
 Explanation: The recursive call is as follows: 
-[3, The maximum value in 2,1,6,0,5] is 6, the left part is [3,2,1], and the right part is [0,5]. 
    -The maximum value in [3,2,1] is 3, the left part is [], and the right part is [2,1]. 
        -Empty array, no child nodes. 
        -The maximum value in [2,1] is 2, the left part is [], and the right part is [1]. 
            -Empty array, no child nodes. 
            -There is only one element, so the child node is a node with a value of 1. 
    -The maximum value in [0,5] is 5, the left part is [0], and the right part is []. 
        -There is only one element, so the child node is a node with a value of 0. 
        -Empty array, no child nodes.

Example 2:

Input: nums = [3,2,1]
 Output: [3,null,2,null,1]

 

prompt:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
  • nums All integers in are  different from each other

Recursion

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        TreeNode* node = new TreeNode(0);
        if(nums.size() == 1){
            node->val = nums[0];
            return node;
        }
        int maxValue = 0;
        int maxValueIndex = 0;  //找最大值
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] > maxValue){
                maxValue = nums[i];
                maxValueIndex = i;
            }
        }
        node->val = maxValue;
        if(maxValueIndex > 0){
            vector<int> newVec(nums.begin(), nums.begin() + maxValueIndex);
            node->left = constructMaximumBinaryTree(newVec);
        }
        if(maxValueIndex < (nums.size()-1)){
            vector<int> newVec(nums.begin() + maxValueIndex + 1, nums.end());
            node->right = constructMaximumBinaryTree(newVec);
        }
        return node;
    }
};

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/113853865