My simple recursive solution, O(1) space


  • 0
    J
     void connect(TreeLinkNode *root) {
        connectNext(root);
    }
    
    void connectNext(TreeLinkNode *node) 
    {
        if (!node) return;
        if (node->left && node->right) 
           node->left->next = node->right; // assign next of left as right  
        if (node->next && node->right) 
            node->right->next = node->next->left; // assign next pointer of right as left of its parent's next
        connectNext(node->left);
        connectNext(node->right);
    }

Log in to reply
 

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