Challenge: Convert Max-Heap to Min-Heap

Given a Max-Heap convert it into a Min-Heap. A solution is placed in the "solution" section for your help, but we would suggest you solve it on your own first.

Problem Statement

Implement a function convertMax(maxHeap) which will convert a binary Max-Heap into a binary Min-Heap. Where maxHeap is a list which is given in the maxHeap format, i.e, the parent is greater than its children.

Output

Returns the converted list.

Sample Input

maxHeap = [9,4,7,1,-2,6,5]

Sample Output

result = [-2,1,5,9,4,6,7]

Have a look at the following diagram

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.