Simple C++ verison, only 19 lines long


  • 0
    M
    void connect(TreeLinkNode *root) {
        TreeLinkNode * parent = root;
        while(parent){
            TreeLinkNode * levHead = NULL;
            TreeLinkNode ** levPrev = &levHead;
            while(parent){
                if(parent->left){
                    *levPrev = parent->left;
                    levPrev = &(parent->left->next);
                }
                if(parent->right){
                    *levPrev = parent->right;
                    levPrev = &(parent->right->next);
                }
                parent = parent->next;
            }
            parent = levHead;
        }
    }

Log in to reply
 

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