Given a Binary Search Tree, the task is to print the nodes of the BST in the following order:
- If the BST contains levels numbered from 1 to N then, the printing order is level 1, level N, level 2, level N – 1, and so on.
- The top-level order (1, 2, …) nodes are printed from left to right, while the bottom level order (N, N-1, …) nodes are printed from right to left.
Approach: To solve the problem, the idea is to store the nodes of BST in ascending and descending order of levels and node values and print all the nodes of the same level alternatively between ascending and descending order. Follow the steps below to solve the problem:
- Initialize a Min Heap and a Max Heap to store the nodes in ascending and descending order of level and node values respectively.
- Perform a level order traversal on the given BST to store the nodes in the respective priority queues.
- Print all the nodes of each level one by one from the Min Heap followed by the Max Heap alternately.
- If any level in the Min Heap or Max Heap is found to be already printed, skip to the next level.
Below is the implementation of the above approach:
25 48 38 28 12 5 20 36 40 30 22 10
Time Complexity: O(V log(V)), where V denotes the number of vertices in the given Binary Tree
Auxiliary Space: O(V)
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 the nodes corresponding to the level value for each level of a Binary Tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- 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)
- Difference between sums of odd level and even level nodes of a Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Print Nodes in Top View of Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print nodes in top view of Binary Tree | Set 2
- Sum of nodes in bottom view of Binary Tree
- Pre-Order Successor of all nodes in Binary Search Tree
- Print all even nodes of Binary Search Tree
- Print all odd nodes of Binary Search 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.