Why runtime error? I can run the code on my computer


  • 0
    X

    the test case of runtime errot is [1,2,null,3]

    class Solution {
    public:
    void flatten(TreeNode* root) {
        if(!root)
            return;
            
        myflatten(root,nullptr);
    }
    
    void myflatten(TreeNode* root,TreeNode* bro)
    {
        if(!root->left&&!root->right&&!bro)
            return;
        if(!root->left&&!root->right)
        {
            root->right=bro;
            myflatten(bro,nullptr);
        }
        else if(root->left&&root->right)
        {
            TreeNode* temp=nullptr;
            
            temp=root->right;
            root->right=root->left;
            myflatten(root->right,nullptr);
            myflatten(root->right,temp);
            
        }
        else if(root->left)
        {
            root->right=root->left;
            root->left=nullptr;
            myflatten(root->right,bro);
        }
        else if(root->right)
        {
            myflatten(root->right,bro);
        }
    }
    
    };

  • 0
    A

    Happened to me as well. For me, my mistake was that I was pointing the left and right pointers of a TreeNode to the same TreeNode.


  • 0
    X

    so you mean it's my mistake, not the OJ's ?


  • 0
    A

    Hey, I verified your code, and it produced a RTE for the case of [1,2,3] as well. Looking in your code for this case, you have:

    temp=root->right;
    root->right=root->left;
    myflatten(root->right,nullptr);
    myflatten(root->right,temp);

    Maybe you forgot to set root->left to null. In this case, root->right and root->left point to the same node. This somehow produces a RTE instead of a wrong ans. Not sure why


  • 0
    X

    thanks for your analysis, it's very kind of you to answer my question so detailedly


Log in to reply
 

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