Run time error due to delete a node?


  • 1
    W

    Hi everyone, I faced a "run time" error returned by leetcode, however I did not see the same error when using VS9. The cause of this "run time" error seems due to "delete cur". But I do not understand why.

    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode *root) {
      
            vector <int> results;
            vector <TreeNode *> stack;
            if(!root) return results;
            
            TreeNode *cur=root;
    
            stack.push_back(cur);
            while(!stack.empty())
            {
                cur=stack.back();
                stack.pop_back();            
                if(cur->left||cur->right)
                {
                    while(cur->left||cur->right)
                    {			
    					TreeNode *tempNode =new TreeNode(cur->val);
    					stack.push_back(tempNode);
                        if(cur->left)
                        {
                            if(cur->right) stack.push_back(cur->right);
                            cur=cur->left;
                        }
                        else
                        {
                            cur=cur->right;
                        }
                    }
                    results.push_back(cur->val);
                }
                else
                {
                    results.push_back(cur->val);  
    				//delete cur; **/*if I uncomment this line, it will cause "run time" error on Leetcode. However the same code has no problem running with VS9.*/**
                }
            }
            return results;
        }
    };

  • 0

    Could you please paste your code in the correct format? Select your code and click on the {} button.


  • 0
    C

    I think you had better not create your own TreeNode. Instead, you should push TreeNode provided by LeetCode into stack.

    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode *root) {
            stack<TreeNode *> s;
            vector<int> result;
            TreeNode *cur = root;
            TreeNode *pre = NULL;  // track previous popped treenode
            while(!s.empty() || cur){
                if(cur){
                    s.push(cur);
                    cur = cur->left;
                }else{
                    TreeNode *t = s.top();
                     // if this node does not have right child or its right child has been popped out 
                    if(!t->right || t->right == pre){  
                        result.push_back(t->val);
                        s.pop();
                        pre = t;
                    }else cur = t->right;  // push right subtree into stack
                }
            }
            return result;
        }
    };

  • 0
    W

    Thanks. I revised the code


  • 0
    W

    @chentao, thanks for sharing your code. But what is the reason "delete cur" causing a "run time" error. I use "TreeNode *tempNode =new TreeNode(cur->val);" to save each father node. Once its children have been completely visited, I pop this father and delete this node.


  • 0
    C

    but you would also delete leaf node which is not created by yourself, which might cause error.


  • 0
    S

    One more thing, could you please format your tags? - is use for combine words, space will treat as tag separator. I think your tag should be run-time-error right?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.