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
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.
- Min-Max Product Tree of a given Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Construct a complete binary tree from given array in level order fashion
- Binary Tree to Binary Search Tree Conversion
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Binary Search Tree | Set 1 (Search and Insertion)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Remove minimum elements from array so that max <= 2 * min
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Print all even nodes of Binary Search Tree
- Print all odd nodes of Binary Search Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Count the Number of Binary Search Trees present in a Binary Tree
- Traverse the matrix in Diagonally Bottum-Up fashion using Recursion
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
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.