Binary tree level order traversal issue

how to do print new line character after each level using pair concept.explain and provide the code

@Rj.25 You can do this with the help of variable only which tell us about the current size of queue.Here is code and explanation:
if (root == NULL) return;

queue<node *> q;  

q.push(root);  

while (!q.empty())  
{  
    int nodeCount = q.size();  

    while (nodeCount > 0) 
    {  
        node *node = q.front();  
        cout << node->data << " ";  
        q.pop();  
        if (node->left != NULL)  
            q.push(node->left);  
        if (node->right != NULL)  
            q.push(node->right);  
        nodeCount--;  
    }  
    cout << endl;  
} 

here nodecount represent the current size of queue and at each level queue size reduces and which help us to print nodes at each level .while nodecount exist it will be the count of one level .
Hope you understand.

@Rj.25 I am closing this doubt and please give rating and feedback so that we can work on that .Feel free to ask.any doubt Happy coding :slight_smile:

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.