Very simple recursive solution


  • 0
        void go(TreeLinkNode* l, TreeLinkNode* r) {
            if (!l) return;
            l->next = r;
            go(l->left, l->right);
            go(l->right, r->left);
            go(r->left, r->right);
        }
            
        void connect(TreeLinkNode *root) {
            if (!root) return;
            go(root->left, root->right);
        }
    

Log in to reply
 

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