Why am i getting MLE?


#1

This code gives MLE

vector Solution::inorderTraversal(TreeNode* A)
{
stack<TreeNode*> s;
vector v;
s.push(A);
while(1)
{
while(1)
{
// if(s.empty()||!s.top())
if(s.empty())
break;
else if(s.top()->left)
s.push(s.top()->left);
else
break;
}
// if(s.empty()||!s.top())
break;
TreeNode* a=s.top();
s.pop();
v.push_back(a->val);
if(a->right)
s.push(a->right);
}
return v;
}

But the code written below gets accepted { Slight changes }
{ You can look at comments int the code to know what are the changes }

vector Solution::inorderTraversal(TreeNode* A)
{
// // s.clear();
// while(!s.empty()) s.pop();
// v.clear();
stack<TreeNode*> s;
vector v;
TreeNode* curr=A;
while(curr||!s.empty())
{
while(curr)
{
s.push(curr);
curr=(s.top()->left);
}
if(s.empty())
break;
TreeNode* a=s.top();
s.pop();
v.push_back(a->val);
if(a->right) curr=a->right;
// s.push(a->right);
}
return v;
}