Skip to content
Related Articles
Print Binary Tree levels in sorted order

Given a Binary tree, the task is to print its all level in sorted order

Examples:

```Input :     7
/    \
6       5
/ \     / \
4  3    2   1
Output :
7
5 6
1 2 3 4

Input :     7
/    \
16       1
/ \
4   13
Output :
7
1 16
4 13
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Here we can use two Priority queue for print in sorted order. We create an empty queue q and two priority queues, current_level and next_level. We use NULL as a separator between two levels. Whenever we encounter NULL in normal level order traversal, we swap current_level and next_level.

 `// CPP program to print levels in sorted order.``#include ``#include ``#include ``using` `namespace` `std;`` ` `// A Binary Tree Node``struct` `Node {``    ``int` `data;``    ``struct` `Node *left, *right;``};`` ` `// Iterative method to find height of Binary Tree``void` `printLevelOrder(Node* root)``{``    ``// Base Case``    ``if` `(root == NULL)``        ``return``;`` ` `    ``// Create an empty queue for level order traversal``    ``queue q;`` ` `    ``// A priority queue (or min heap) of integers for ``    ``// to store all elements of current level. ``    ``priority_queue<``int``, vector<``int``>, greater<``int``> > current_level;`` ` `    ``// A priority queue (or min heap) of integers for ``    ``// to store all elements of next level. ``    ``priority_queue<``int``, vector<``int``>, greater<``int``> > next_level;`` ` `    ``// push the root for traverse all next level nodes``    ``q.push(root);`` ` `    ``// for go level by level``    ``q.push(NULL);`` ` `    ``// push the first node data in previous_level queue``    ``current_level.push(root->data);`` ` `    ``while` `(q.empty() == ``false``) {`` ` `        ``// Get top of priority queue ``        ``int` `data = current_level.top();`` ` `        ``// Get top of queue``        ``Node* node = q.front();`` ` `        ``// if node == NULL (Means this is boundary``        ``// between two levels), swap current_level``        ``// next_level priority queues.``        ``if` `(node == NULL) {``            ``q.pop();`` ` `            ``// here queue is empty represent``            ``// no element in the actual``            ``// queue``            ``if` `(q.empty())``                ``break``;`` ` `            ``q.push(NULL);``            ``cout << ``"\n"``;`` ` `            ``// swap next_level to current_level level``            ``// for print in sorted order``            ``current_level.swap(next_level);`` ` `            ``continue``;``        ``}`` ` `        ``// print the current_level data``        ``cout << data << ``" "``;`` ` `        ``q.pop();``        ``current_level.pop();`` ` `        ``/* Enqueue left child */``        ``if` `(node->left != NULL) {``            ``q.push(node->left);`` ` `            ``// Enqueue left child in next_level queue``            ``next_level.push(node->left->data);``        ``}`` ` `        ``/*Enqueue right child */``        ``if` `(node->right != NULL) {``            ``q.push(node->right);`` ` `            ``// Enqueue right child in next_level queue``            ``next_level.push(node->right->data);``        ``}``    ``}``}`` ` `// Utility function to create a new tree node``Node* newNode(``int` `data)``{``    ``Node* temp = ``new` `Node;``    ``temp->data = data;``    ``temp->left = temp->right = NULL;``    ``return` `temp;``}`` ` `// Driver program to test above functions``int` `main()``{``    ``// Let us create binary tree shown in above diagram``    ``Node* root = newNode(7);``    ``root->left = newNode(6);``    ``root->right = newNode(5);``    ``root->left->left = newNode(4);``    ``root->left->right = newNode(3);``    ``root->right->left = newNode(2);``    ``root->right->right = newNode(1);`` ` `    ``/*     7``         ``/    \``        ``6       5``       ``/ \     / \``      ``4  3     2  1          */`` ` `    ``cout << ``"Level Order traversal of binary tree is \n"``;``    ``printLevelOrder(root);``    ``return` `0;``}`
Output:

```Level Order traversal of binary tree is
7
5 6
1 2 3 4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up