Leetcode Problem Solving Report Largest Binary Tree Series

Leetcode's largest binary tree series

654. Largest Binary Tree

Given an integer array without repeated elements. A maximum binary tree constructed from this array is defined as follows:

  • The root of the binary tree is the largest element in the array.
  • The left subtree is the largest binary tree constructed from the left part of the maximum value in the array.
  • The right subtree is the largest binary tree constructed from the right part of the maximum value in the array.
  • Construct the largest binary tree from the given array and output the root node of this tree.
输入:[3,2,1,6,0,5]
输出:返回下面这棵树的根节点:

      6
    /   \
   3     5
    \    / 
     2  0   
       \
        1

answer:

The idea is very simple, first find the maximum value, then put the left subtree on the left and the right subtree on the right. As for why I want to write a blog, because I feel that I write iteratively better than recursively, so I want to try recursive writing

/**
 * 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* construct(vector<int>&nums,int l,int r){
    
    
        int maxnum=l;
        if(l>r)
            return nullptr;
        for(int i=l+1;i<=r;++i){
    
    
            if(nums[i]>nums[maxnum])
                maxnum=i;
        }
        TreeNode* temp=new TreeNode(nums[maxnum]);
        temp->left=construct(nums,l,maxnum-1);
        temp->right=construct(nums,maxnum+1,r);
        return temp;
    }
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    
    
        TreeNode* ans=construct(nums,0,nums.size()-1);
        return ans;
    }
};

998. The Largest Binary Tree II

Maximum tree definition: a tree in which the value of each node is greater than any other value in its subtree.

Give the root node root of the largest tree.

As in the previous question, the given tree is recursively constructed from table A (root = Construct(A)) using the following Construct(A) routine:

  • If A is empty, return null
  • Otherwise, let A[i] be the largest element of A. Create a root node root whose value is A[i]
  • The left subtree of root will be constructed as Construct([A[0], A[1], …, A[i-1]])
  • The right subtree of root will be constructed as Construct([A[i+1], A[i+2], …, A[A.length-1]])
  • Return to root

Please note that we did not directly give A, only one root node root = Construct(A).

Suppose that B is a copy of A, and the value val is appended. Ensure that the values ​​in B are different.

Return Construct(B).

answer:

This question is a transformation of the previous question. Insert a value at the end to let you reconstruct this tree. I think it is a search tree here. I thought about it for a long time. Easy to step on

(Ps: The recursive code is really simple...)

/**
 * 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* insertIntoMaxTree(TreeNode* root, int val) {
    
    
        if(val>=root->val){
    
    
            root=new TreeNode(val,root,nullptr);
        }
        else {
    
    
            if(root->right==nullptr)
                root->right=new TreeNode(val);
            else
                root->right=insertIntoMaxTree(root->right,val);
        }
        return root;
    }
};

Guess you like

Origin blog.csdn.net/qq_43477024/article/details/111939576