Given a Binary Search Tree (BST), the task is to print the BST in min-max fashion.
What is min-max fashion?
A min-max fashion means you have to print the maximum node first then the minimum then the second maximum then the second minimum and so on.
Input: 100 / \ 20 500 / \ 10 30 \ 40 Output: 10 500 20 100 30 40 Input: 40 / \ 20 50 / \ \ 10 35 60 / / 25 55 Output: 10 60 20 55 25 50 35 40
- Create an array inorder and store the inorder traversal of the givrn binary search tree.
- Since the inorder traversal of the binary search tree is sorted in ascending, initialise i = 0 and j = n – 1.
- Print inorder[i] and update i = i + 1.
- Print inorder[j] and update j = j – 1.
- Repeat steps 3 and 4 until all the elements have been printed.
Below is the implementation of the above approach:
20 80 30 70 40 60 50
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Print all odd nodes of Binary Search Tree
- Print all even nodes of Binary Search Tree
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Construct a complete binary tree from given array in level order fashion
- Minimum swap required to convert binary tree to binary search tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Count the Number of Binary Search Trees present in a Binary Tree
- Difference between Binary Tree and Binary Search Tree
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- Iterative Search for a key 'x' in Binary Tree
- Search a node in Binary Tree
- Make Binary Search Tree
- Binary Search Tree | Set 2 (Delete)
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.