C++ solution( TLE probelm resolved)


#1

TreeNode *helper(TreeNode **root,const vector&a,int s,int e)
{
if(s>e)
{
return NULL;
}
int mid=(s+e)/2;
(*root)=new TreeNode(a[mid]);
(*root)->left=helper(&(*root)->left,a,s,mid-1);
(*root)->right=helper(&(*root)->right,a,mid+1,e);
return (root);
}
TreeNode
Solution::sortedArrayToBST(const vector &A)
{
if(A.size()==0)
{
return NULL;
}
TreeNode *root=helper(&root,A,0,A.size()-1);
return root;
}