4 lines, Short but Clear C++ Code, O(1) Space (No recursion/stack/queue)


  • 0
    F
    struct Solution {
        void connect(TreeLinkNode *root) { 
            for(;root && root->left; root = root->left)
                for (auto cur = root; cur; cur=cur->next)
                {
                    cur->left->next = cur->right;
                    if (cur->next)  cur->right->next = cur->next->left;
                }
            }
    };

Log in to reply
 

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