题目
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
把升序的数组,转换为平衡BST
1 理解平衡的BST的含义。
2 一样是递归,从当中mid分割。mid作为root,比mid小的在左边;比mid大的在右边。
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
if(num.length==0){
return null;
}
return useme(num,0,num.length-1);
}
public TreeNode useme(int[] num , int start, int end ){
if(start<=end){
int mid = start +(end - start)/2;
TreeNode mit = new TreeNode(num[mid]);
mit.left = useme(num,start,mid-1);
mit.right = useme(num,mid+1,end);
return mit;
}
return null;
}
}
1 理解