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
- 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
- Max Heap in Python
- Binary Heap
- Skew Heap
- Min Heap in Python
- Binomial Heap
- Pairing Heap
- Max Heap in Java
- K’th Least Element in a Min-Heap
- C++ Program for Heap Sort
- Print all nodes less than a value x in a Min 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 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.