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.
We'll cover the following
Problem Statement #
Implement a function convertMax(maxHeap)
which will convert a binary max-heap into a binary min-heap where maxHeap
is an array which is given in the maxHeap
format, i.e the parent is greater than its children.
Input #
A Max-Heap
Output #
Returns the converted array
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 :
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy