Given a Binary tree, the task is to print its all level in sorted order
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
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.
Level Order traversal of binary tree is 7 5 6 1 2 3 4
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print a Binary Tree in Vertical Order | Set 1
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Print extreme nodes of each level of Binary Tree in alternate order
- Print the nodes at odd levels of a tree
- Averages of Levels in Binary Tree
- Reverse alternate levels of a perfect binary tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Print array of strings in sorted order without copying one string into another
- Insertion in a Binary Tree in level order
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.