Connect same level nodes

I have written the code for printing same level nodes and taking input, but i am unable to understand how to write the code for printing output. Please guide me or give me the basic code.

hello @kshitijkameria
Do a level order traversal , insert a value Null in the queue that represent the end of a particular level.In level order traversal , when a particular node is poped , check if queue is empty or not.
If queue is not empty and its front is not NULL , then make current node right equal to queue.front() and push its child in queue.
If null occurs in level order traversal then if queue is not empty then push null else just break the loop.

void connect_nextRight(Node* root) {
    queue<Node*> qu;
    qu.push(root); qu.push(NULL);
    while(!qu.empty()){
        Node* n = qu.front();
        qu.pop();
        if(n != NULL){
            if(n->left) qu.push(n->left);
            if(n->right) qu.push(n->right);

            n->nextRight = qu.front();
        }
        else {
            if(!qu.empty()) qu.push(NULL);
        }
    }
    return ; 
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

The code was never a problem. I am not able to understand the code for printing the output

@kshitijkameria see this way you can print> cout<<“Following are populated nextRight pointers in the tree(-1 is printed if there is no nextRight”;

    int a = tree->root->nextRight != null ? tree->root->nextRight->data : -1; 

    cout<<"nextRight of "<<tree->root->data<<" is "<<a; 

    int b = tree->root->left->nextRight != null ? tree->root->left->nextRight->data : -1; 

    cout<<"nextRight of "<<tree.root.left.data<<" is "<<b; 

    int c = tree->root->right->nextRight != null ? tree->root->right->nextRight->data : -1; 

    cout<<"nextRight of "<<tree->root->right->data<<" is "<<c; 

    int d = tree->root->left->left->nextRight != null ? tree->root->left->left->nextRight->data : -1; 

    cout<<"nextRight of "<<tree->root->left->left->data<<" is "<<d; `Preformatted text`

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.