Given a Binary Search Tree which is also a Complete Binary Tree. The problem is to convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap.
Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap. All the nodes in the Max Heap satisfies the given condition, that is, values in the left subtree of a node should be less than the values in the right subtree of the node.
1. Create an array arr of size n, where n is the number of nodes in the given BST.
2. Perform the inorder traversal of the BST and copy the node values in the arr in sorted
3. Now perform the postorder traversal of the tree.
4. While traversing the root during the postorder traversal, one by one copy the values from the array arr to the nodes.
Postorder Traversal of Tree: 1 2 3 4 5 6 7
Time Complexity: O(n)
Auxiliary Space: O(n)
where, n is the number of nodes in the tree
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
- Convert min Heap to max Heap
- Convert BST to Min Heap
- In-place Convert BST into a Min-Heap
- Heap Sort for decreasing order using min heap
- K-ary Heap
- Pairing Heap
- Weak Heap
- Max Heap in Java
- K’th Least Element in a Min-Heap
- Binary Heap
- Max Heap in Python
- Binomial Heap
- Min Heap in Python
- Skew Heap
- Implementation of Binomial Heap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.