My solution using c++,short code


  • 0
    H
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if(!root) return;
            TreeLinkNode nextDummy(0);
            TreeLinkNode *nextHead = &nextDummy;
            TreeLinkNode *curr = root;
            while(curr){
                if(curr->left) {
                    nextHead->next = curr->left;
                    nextHead = nextHead->next;
                }
                if(curr->right) {
                    nextHead->next = curr->right;
                    nextHead = nextHead->next;
                }
                curr = curr->next;
            }
            connect(nextDummy.next);
        }
    };
    

Log in to reply
 

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