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