Easy BFS solution CPP


#1
void Solution::connect(TreeLinkNode* A) {
    if(!A) return;
    queue<TreeLinkNode*> q;
    q.push(A);
    
    while(!q.empty()){
        
        int n = q.size();
        TreeLinkNode* prev = NULL;
        
        for(int i=0;i<n;i++){
            TreeLinkNode* curr = q.front(); q.pop();
            
            if(curr->left) q.push(curr->left);
            if(curr->right) q.push(curr->right);
            
            curr->next = NULL;
            if(prev!=NULL) prev->next = curr;
            
            prev = curr;
        }

    }
}