Short, Simple to Understand and Fast C++ Solution


#1

TreeNode* solve(const vector &A, int start, int end) {
if(start>end)
return NULL;
if(start == end)
return new TreeNode(A[start]);
int mid = (start+end)/2;
TreeNode *node = new TreeNode(A[mid]);
node->left = solve(A, start, mid-1);
node->right = solve(A, mid+1, end);
return node;
}

TreeNode* Solution::sortedArrayToBST(const vector &A) {
TreeNode *ans = solve(A, 0, A.size()-1);
return ans;
}