Simple well commented solution with appropriate names meeting question constraints


#1
void Solution::connect(TreeLinkNode* A) {
    
    TreeLinkNode* parent = A;
    
    while(paren!=NULL)
    {
        //find first child
        TreeLinkNode* fir_chi = NULL;
        TreeLinkNode* temp_par = parent;
        while(temp_par!=NULL)
        {
            if(temp_par->left!=NULL){
                fir_chi = temp_par->left;
                break;
            }
            if(temp_par->right!=NULL){
                fir_chi = temp_par->right;
                break;
            }
            temp_par = temp_par->next;
        }
        
        //map children to their next nodes
        temp_par = parent;
        TreeLinkNode* prev_sib = NULL;
        while(temp_par!=NULL)
        {
            if(temp_par->left!=NULL)
            {
                //check if it is the first child else map
                if(prev_sib==NULL)prev_sib = temp_par->left;
                else{
                    prev_sib->next = temp_par->left;
                    prev_sib = prev_sib->next;
                }
            }
            if(temp_par->right!=NULL)
            {
                //check if it is the first child else map
                if(prev_sib==NULL)prev_sib = temp_par->right;
                else{
                    prev_sib->next = temp_par->right;
                    prev_sib = prev_sib->next;
                }                
            }
            temp_par = temp_par->next;
        }
        if(prev_sib!=NULL)prev_sib->next=NULL;
        
        //shift level
        parent = fir_child;
    }
}