34/61 test cases passed


  • 0
    S

    Blockquote

    34 / 61 test cases passed.
    Status: Wrong Answer
    Submitted: 0 minutes ago
    Input:	{2,1,3,0,7,9,1,2,#,1,0,#,#,8,8,#,#,#,#,7}
    Output:	{2,#,1,3,#,0,7,9,1,#,2,1,0,#,7,#}
    Expected:	{2,#,1,3,#,0,7,9,1,#,2,1,0,8,8,#,7,#}
    

    Blockquote

    what is wrong in this simple code

    struct TreeLinkNode *GetNode(struct TreeLinkNode * node){

    if(node==NULL)
    	return NULL;
    
    
    if(node->left==NULL && node->right==NULL)
    	return GetNode(node->next);
    
    if(node->left)
    	return node->left;
    
    if(node->right)
    	return node->right;
    

    }

    void connect(struct TreeLinkNode *root) {

    if(root) {
    
    	if(root->left)
    	{
    	    if(root->right)
    		    root->left->next=root->right;
    	    else
    		    root->left->next=GetNode(root->next);
    	}
    
    	if(root->right)
    		root->right->next=GetNode(root->next);
    
    
        connect(root->left);
        connect(root->right);
    }
    

    }


  • 1
    S
    > > > Got the bug:
    > > 
    > > first right subtree should be travese   else it might be a case when
    > > right subtree next pointer is not proccesed
    > > 
    

    instead of

    connect(root->left);
      connect(root->right);
    

    use

       connect(root->right);   
        connect(root->left);
    > > 
    > > Full code :
    

    struct TreeLinkNode *GetNode(struct TreeLinkNode * node){

    if(node==NULL)
    	return NULL;
    
    
    if(node->left==NULL && node->right==NULL)
    	return GetNode(node->next);
    
    if(node->left)
    	return node->left;
    
    if(node->right)
    	return node->right;
    

    }

    void connect(struct TreeLinkNode *root) {

    if(root) {
    
    	if(root->left)
    	{
    	    if(root->right)
    		    root->left->next=root->right;
    	    else
    		    root->left->next=GetNode(root->next);
    	}
    
    	if(root->right)
    		root->right->next=GetNode(root->next);
    
        connect(root->right);//first right subtree should be travese 
                                //else it might be a case when right subtree next pointer is not proccesed
        connect(root->left);
    
    }
    

    }


Log in to reply
 

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