题目
方法一:递归
class Solution {
int[] num = null;
public TreeNode constructMaximumBinaryTree(int[] nums) {
num = nums;
return myTree(0,num.length-1);
}
public TreeNode myTree( int begin , int end){
if(begin > end) return null;
int maxIndex = max(begin, end);
TreeNode root = new TreeNode(num[maxIndex]);
root.left = myTree(begin,maxIndex-1);
root.right = myTree(maxIndex+1,end);
return root;
}
public int max(int left,int right){
int max = -1;
int index = -1;
for(int i = left;i<=right;i++){
if(num[i] > max) {
max = num[i];
index = i;
}
}
return index;
}
}