/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return build(nums,0,nums.length-1);
}
//构造一个函数用来控制最大值以及左右数组索引
public TreeNode build(int []nums,int lo,int hi){
if(lo>hi){
return null;
}
int index=-1,maxVal=Integer.MIN_VALUE;
for(int i=lo;i<=hi;i++){
if(maxVal<nums[i]){
maxVal=nums[i];
index=i;
}
}
TreeNode root=new TreeNode(maxVal);
root.left=build(nums,lo,index-1);
root.right=build(nums,index+1,hi);
return root;
}
}