What is the origin of this "run time error" for {3,9,20,#,#,15,7}, it works on my machine?


  • 0
    X

    This is a simple level scheme. I precompute the "first node for the next level" in nextrunner. The test case {3,9,20,#,#,15,7} works on my machine but it failed to run on LC. Why?

        void connect(TreeLinkNode *root) {
            if(root==NULL)return;
            TreeLinkNode* runner=root;
            TreeLinkNode* nextrunner=FindNext(runner);
            while(nextrunner){//set next layer
                while(runner){//move horizontally
                    if(runner->left!=NULL and runner->right!=NULL){
                        runner->left->next=runner->right;
                        runner->right->next=FindNext(runner->next);
                    }
                    else if(runner->left!=NULL){
                        runner->left=FindNext(runner->next);
                    }
                    else if(runner->right!=NULL){
                        runner->right->next=FindNext(runner->next);
                    }
                    runner=runner->next;
                }
                runner=nextrunner;
                nextrunner=FindNext(runner);
            }
        }
    
        TreeLinkNode* FindNext(TreeLinkNode* current){
            while(current){
                if(current->left){return current->left;}
                if(current->right){return current->right;}
                current=current->next;
            }
            return NULL;
        }

Log in to reply
 

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