ou are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using th

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

ou are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using th

Post by answerhappygod »

ou are given an integer array nums with no duplicates. A maximum
binary tree can be built recursively from nums using the following
algorithm: Create a root node whose value is the maximum value in
nums. Recursively build the left subtree on the subarray prefix to
the left of the maximum value. Recursively build the right subtree
on the subarray suffix to the right of the maximum value. Return
the maximum binary tree built from nums.
/**
* 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) {

}
};
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply