Runtime 8ms in C :)


  • 0
    S

    // Initially, all next pointers are set to NULL.

    // Initially, all next pointers are set to NULL.
    void contect_Right_Pointer(struct TreeLinkNode *Lnode, struct TreeLinkNode Rnode,struct TreeLinkNode Pnode){

    if(Lnode && Pnode!=NULL)
        Lnode->next=Pnode->right;
    if(Rnode && Pnode!=NULL && Pnode->next!=NULL)
        Rnode->next=Pnode->next->left;
    
    
    
     if(Lnode)
    	 contect_Right_Pointer(Lnode->left,Lnode->right,Lnode);
     if(Rnode)
    	 contect_Right_Pointer(Rnode->left,Rnode->right ,Rnode);
    

    }

    void connect(struct TreeLinkNode *root) {

    if(root==NULL)
    return ;
    contect_Right_Pointer(root->left,root->right, root);
    }


  • 0
    X

    i'm afraid it is not o(1) space solution. you have ignored the stack space used in recursion.


Log in to reply
 

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