# Why Memory limit exceed in my code

#1
``````void insert(TreeNode* node, const vector<int> &A, int s, int m, int e) {
if(s>e)
return;
int lmid = (m-s)/2+s;
int rmid = (e-m)/2+m;
if(lmid >= 0) {
TreeNode* newNode1 = new TreeNode(A[lmid]);
node->left = newNode1;
insert(node->left, A, s, lmid, m-1);
}
if(rmid >= 0) {
TreeNode* newNode2 = new TreeNode(A[rmid]);
node->right =newNode2;
insert(node->right, A, m+1, rmid, e);
}
}

TreeNode* Solution::sortedArrayToBST(const vector<int> &A) {
int s = 0;
int e = A.size()-1;
int m = (e-s)/2+s;
TreeNode* root = new TreeNode(A[m]);
insert(root, A, s, m, e);
return root;
}
``````

dsdsa

#2

A.size( ) returns a unsigned integer so when A.size()=0 then the result of A.size()-1 is (0-1)%(UINT_MAX+1) which is UINT_MAX which results to function call stack overflow . so write int(A.size)-1 in place of A.size()-1

#3

This is not working to replace A.size()-1 by (A.size())-1, still getting memory limit exceeded

#4

You are not handling the edge case when A.size() = 0, your m would be 0 which is causing MLE because A is empty and there is no element in A.