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
- Sum of k largest elements in BST
- Program for Preemptive Priority CPU Scheduling
- Skew Heap
- Convert BST to Min Heap
- Array Representation Of Binary Heap
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
- In-place Convert BST into a Min-Heap
- Convert min Heap to max Heap
- Check if a given Binary Tree is Heap
- Fibonacci Heap | Set 1 (Introduction)
- Where is Heap Sort used practically?
- Binary Heap
- Tournament Tree (Winner Tree) and Binary Heap
- Inorder Successor in 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 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.