974. Check if an array represents a min heap

Given an array of integers nums. Check whether the array represents a binary min-heap or not. Return true if it does, otherwise return false.

A binary min-heap is a complete binary tree where the key at the root is the minimum among all keys present in a binary min-heap and the same property is recursively true for all nodes in a Binary Tree.

Example 1:

Input: nums = [10, 20, 30, 21, 23]

Output: true

Explanation: Each node has a lower or equal value than its children.

Example 2:

Input: nums = [10, 20, 30, 25, 15]

Output: false

Explanation: The node with value 20 has a child with value 15, thus it is not a min-heap.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 2, 1, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • The array represents a complete binary tree.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
bool isHeap(vector<int>& nums) {
 
}
};
Test Case

Input:

Nums