A Simple Explaination and A Short piece of Codes

  • 0

    We should use the connected nodes on the upper level to knit the nodes on the current level.

    In the following codes, 'up' is the upper node, 'down' is the current node that under 'up', and 'nexthead' is the left-most node we start to knit the list on the next level.

    class Solution {
        void connect(TreeLinkNode *root) {
            if(root == NULL)return;
            root->next = NULL;
            TreeLinkNode *up = root, *down, *nexthead = root->left;
            while(nexthead != NULL)
                down = nexthead;
                    down->next = up->right;
                    down = down->next;
                    up = up->next;
                    down->next = up->left;
                    down = down->next;
                down->next = NULL;
                up = nexthead;
                nexthead = nexthead->left;

Log in to reply

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