【发布时间】:2018-09-07 01:33:26
【问题描述】:
以下代码执行二叉树中序遍历。当我在 Leetcode 中执行它时,我会收到一个Run Status Code: Memory Limit Exceeded。有人可以解释导致此错误的原因吗?
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL)
return res;
stack<TreeNode*> st;
st.push(root);
while(st.empty()==0){
TreeNode* cur=st.top();
st.pop();
if(cur->right!=NULL)//right childtree is not NULL,push
st.push(cur->right);
if(cur->left!=NULL){//left childtree is not NULL,push
st.push(cur);
st.push(cur->left);
}
else //if left child tree is NULL,store the value
res.push_back(cur->val);
}
//inorder(root,res);
return res;
}
【问题讨论】:
-
您的意见是什么?你的程序的其余部分在哪里?尝试将minimal reproducible example 放在一起。