Given a tree, print the level order traversal 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
We have discussed a priority queue based solution in below post.
In this post, a set (which is implemented using balanced binary search tree) based solution is discussed.
1. Start level order traversal of tree.
2. Store all the nodes in a set(or any other similar data structures).
3. Print elements of set.
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.
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print Binary Tree levels in sorted order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print alternate nodes from all levels of a Binary Tree
- Print all Palindromic Levels Of a Binary Tree
- Print all K-sum levels in a Binary Tree
- Print all Prime Levels of a Binary Tree
- Print all Co-Prime Levels of a Binary Tree
- Print all Exponential Levels of a Binary Tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Reverse alternate levels of a perfect binary tree using Stack
- Print the nodes at odd levels of a 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.