Level Order with two pointer to simulate queue


  • 0
    Y
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if(!root)
                return ;
            TreeLinkNode* mark = new TreeLinkNode(-1);
            TreeLinkNode* cur = root;
            TreeLinkNode *front = root, *back = mark;
            front -> next = back;
            while(front){
                if(front->left){
                    back->next = front->left;
                    back = front->left;
                }
                if(front->right){
                    back->next = front->right;
                    back = front->right;
                }
                if(front->next == mark){
                    front -> next = NULL;
                    front = mark->next;
                    back->next = mark;
                    back = mark;
                }else
                    front = front->next;
            }
            free(mark);
        }
    };
    

Log in to reply
 

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