<p>You are given an integer array <code>nums</code> with no duplicates. A <strong>maximum binary tree</strong> can be built recursively from <code>nums</code> using the following algorithm:</p>

<ol>
	<li>Create a root node whose value is the maximum value in <code>nums</code>.</li>
	<li>Recursively build the left subtree on the <strong>subarray prefix</strong> to the <strong>left</strong> of the maximum value.</li>
	<li>Recursively build the right subtree on the <strong>subarray suffix</strong> to the <strong>right</strong> of the maximum value.</li>
</ol>

<p>Return <em>the <strong>maximum binary tree</strong> built from </em><code>nums</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/tree1.jpg" style="width: 302px; height: 421px;" />
<pre>
<strong>Input:</strong> nums = [3,2,1,6,0,5]
<strong>Output:</strong> [6,3,5,null,2,0,null,null,1]
<strong>Explanation:</strong> The recursive calls are as follow:
- The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
    - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
        - Empty array, so no child.
        - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
            - Empty array, so no child.
            - Only one element, so child is a node with value 1.
    - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
        - Only one element, so child is a node with value 0.
        - Empty array, so no child.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/tree2.jpg" style="width: 182px; height: 301px;" />
<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> [3,null,2,null,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
	<li>All integers in <code>nums</code> are <strong>unique</strong>.</li>
</ul>

/**
 * 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 segmentTree{
    public:
    segmentTree* left,*right;
    int low,high;
    int maxElement;
    segmentTree(int a,int b,int c){
        low =a;
        high=b;
        maxElement = c;
        left=NULL;
        right=NULL;

    }
 };
     segmentTree* createTree(vector<int> &nums,int lowIndex,int highIndex){
        if(lowIndex>highIndex) return NULL;
        int temp=0;
        for(int i=lowIndex;i<=highIndex;i++) temp=max(temp,nums[i]);
        segmentTree* head = new segmentTree(lowIndex,highIndex,temp);
        if(lowIndex==highIndex) return head;
        head->left = createTree(nums,lowIndex,(lowIndex+highIndex)/2);
        head->right = createTree(nums,(lowIndex+highIndex)/2+1,highIndex);
        return head;

    }
    int getMax(int lowIndex,int highIndex,segmentTree* head){
        if(head==NULL) return 0;
        if(head->low>=lowIndex&&head->high<=highIndex) return head->maxElement;
        if(head->high<lowIndex||head->low>highIndex) return 0;
        return max(getMax(lowIndex,highIndex,head->left),getMax(lowIndex,highIndex,head->right));
    }
TreeNode* createTree1(vector<int> &nums,int low,int high,segmentTree* head,map<int,int> &index){
    if(low> high) return NULL;
    
    int maxElement= getMax(low,high,head);
    int maxElementIndex = index[maxElement];
    // cout<<low<<high<<maxElement<<maxElementIndex<<endl;
    TreeNode* currNode = new TreeNode(maxElement);
    currNode-> left = createTree1(nums,low,maxElementIndex-1,head,index);
    currNode-> right = createTree1(nums,maxElementIndex+1,high,head,index);
    return currNode;
}
void inorder(segmentTree* head){
    if(head==NULL) return ;
    inorder(head->left);
    cout<<"low high max are "<<head->low<<" "<<head->high<<" "<<head->maxElement<<endl;
    inorder(head->right);
}
class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        segmentTree* head = createTree(nums,0,nums.size()-1);
        map<int,int> index;
        for(int i=0;i<nums.size();i++) index[nums[i]]= i;
        // inorder(head);
        // return NULL;
        return createTree1(nums,0,nums.size()-1,head,index);
    }
};
54731