Showing Memory Limit Exceeded for Recursive method

amazon
Tags: #<Tag:0x00007f1825a7a140>

#1

Please check my code , it is showing memory limit exceeded.
Also in which cases do we get MLE error?
int func(TreeNode * l , TreeNode* r){
if(l == NULL && r == NULL)
return 1;
if(l == NULL || r == NULL)
return 0;

return ( func(l->left , r->right) && func(l->right , r->left) && (l->val == r->val));

}
int Solution::isSymmetric(TreeNode* A) {
if(A->left == NULL && A->right== NULL)
return 1;
if(A->left == NULL || A->right == NULL)
return 0;
return func(A->left , A->right);
}


#2

MY CODE IS ALMOST EQUIVALENTS TO YOURS , but it got accepted .
int check(TreeNode* A,TreeNode* B){
if(A==NULL && B!=NULL){
return 0;
}
else if(B==NULL && A!=NULL){
return 0;
}
else if(A==NULL && B==NULL){
return 1;
}
if(A->val==B->val){
return (check(A->left,B->right) && check(A->right,B->left));
}
else{
return 0;
}
}
int Solution::isSymmetric(TreeNode* A) {
if(A==NULL){
return 1;
}
return check(A->left,A->right);
}


#3

Make sure that the input format is properly followed. I forgot to add “-1” for one of the leaf node of the tree and hence got the error as “Memory Limit Exceeded”. :slight_smile: