C++ with scroll queue


  • 0
    T
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if (root==NULL) return ;
            queue<TreeLinkNode*> q[2];
            queue<int>qindex;
            int now=0;
            q[now].push(root);
            TreeLinkNode* tmpnode(0);
            while (!q[now].empty())
            {
                while (!q[now].empty())
                {
                    tmpnode=q[now].front();
                    q[now].pop();
                    if (tmpnode->left!=NULL) q[1-now].push(tmpnode->left);
                    if (tmpnode->right!=NULL) q[1-now].push(tmpnode->right);
                    if (!q[now].empty())
                        tmpnode->next=q[now].front();
                }
                now=1-now;
            }
        }
    };
    

Log in to reply
 

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