只有不断取中位数,才能确保高度最低。
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &A) {
TreeNode *result = buildBST(A,0,A.size()-1);
return result;
}
TreeNode *buildBST(vector<int> &A, int start, int end){
if(start > end){return NULL;}
int mid = (start + end)/2;
TreeNode *p = new TreeNode(A[mid]);
p->left = buildBST(A,start,mid-1);
p->right = buildBST(A,mid+1,end);
return p;
}
};