Why for the input A : [ 1, 4, 8 ]
, the output is shown as:
The given tree is not balanced!
How to debug it here.
The given tree is not balanced!
This code works well for all cases.
void bst(const vector<int> &A,TreeNode* &ans, int l,int r )
{
if(l>r)
return;
int mid= (l+r)/2;
ans = new TreeNode(A[mid]);
bst(A, ans->left, l,mid-1);
bst(A, ans->right, mid+1,r);
}
TreeNode* Solution::sortedArrayToBST(const vector<int> &A) {
int l = 0;
int r = A.size()-1;
TreeNode* ans;
bst(A, ans,l,r);
return ans;
}