class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return make(nums, 0, nums.length-1);
}
public static TreeNode make(int[] a, int begin, int end){
if(begin >= end){
int target = (begin + end) / 2;
TreeNode node1 = new TreeNode(a[target]);
return node1;
}
//1. 找到最大的元素的位置
int index = begin;
for(int i=begin; i<=end; i++){
if(a[i] > a[index]) index = i;
}
TreeNode node1 = new TreeNode(a[index]);
if(index !=begin){
//2.找左边
node1.left = make(a, begin, index-1);
}
if(index != end){
//3. 找右边
node1.right = make(a, index+1, end);
}
return node1;
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}