public class Solution {

private int getMax(int []arr,int l,int r){

int maxIndex = -1,max=Integer.MIN_VALUE;

for(int i=l;i<=r;i++){

if(max<arr[i]){

maxIndex=i;

max=arr[i];

}

}

return maxIndex;

}

public TreeNode constructMaximumBinaryTree(int[] nums) {

if(nums.length==0)

return null;

TreeNode root= constructTree(nums,0,nums.length-1);
return root;
}
private TreeNode constructTree(int []nums,int l,int r){
if(l>r)
return null;
int maxIdx = getMax(nums,l,r);
TreeNode treeNode = new TreeNode(nums[maxIdx]);
treeNode.left= constructTree(nums,l,maxIdx-1);
treeNode.right = constructTree(nums,maxIdx+1,r);
return treeNode;
}

}