Simple recursive c++ solution using preorder traversal


  • 0
    G
    void connect(TreeLinkNode *root) {
        if(!root || !root->left || !root->right)
            return;
        
        root->left->next = root->right;
        if(root->next)
            root->right->next = root->next->left;
        else
            root->right->next = NULL;
        
        connect(root->left);
        connect(root->right);
    }

Log in to reply
 

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