Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
answer:
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return myATBT(nums,0,nums.size() - 1);
}
TreeNode * myATBT(vector<int> & nums,int start,int end){
if(start > end || start < 0 || end >= nums.size())
return NULL;
int mid = (start + end) / 2;
TreeNode * root = new TreeNode(nums[mid]);
root->left = myATBT(nums,start,mid - 1);
root->right = myATBT(nums,mid + 1,end);
return root;
}
};