![]() |
VOOZH | about |
Given a tree, print the level order traversal 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
We have discussed a priority queue based solution in below post.
Print Binary Tree levels in sorted order | Set 1 (Using Priority Queue)
In this post, a set (which is implemented using balanced binary search tree) based solution is discussed.
Approach :
Implementation:
7 5 6 1 2 3 4
Time Complexity: O(n*log(n)) where n is the number of nodes in the binary tree.
Auxiliary Space: O(n) where n is the number of nodes in the binary tree.