Super eay using recursion


#1

TreeNode* sortedArrayToBSTHelper(int a[], int start, int end) {
//base case
if(start > end) {
return NULL;
}
int mid = (start + end) / 2;
TreeNode* head = new TreeNode(a[mid]);
head -> left = sortedArrayToBSTHelper(a, start, mid - 1);
head -> right = sortedArrayToBSTHelper(a, mid + 1, end);
return head;
}

TreeNode* Solution::sortedArrayToBST(const vector &A) {
int a[A.size()];
for(int i = 0; i < A.size(); i++) {
a[i] = A[i];
}
return sortedArrayToBSTHelper(a, 0, A.size() - 1);
}