Open In App

Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given the level order traversal of a Complete Binary Tree, determine whether the Binary Tree is a valid Min-Heap

Examples:

Input: level = [10, 15, 14, 25, 30]
Output: True
Explanation: The tree of the given level order traversal is

Given-level-order-traversal-of-a-Binary-Tree-check-if-the-Tree-is-a-MinHeap-1

Given Tree is Min-Heap

We see that each parent has a value less than its child, and hence satisfies the min-heap property

Input: level = [30, 56, 22, 49, 30, 51, 2, 67]
Output: False
Explanation: The tree of the given level order traversal is

Given-level-order-traversal-of-a-Binary-Tree-check-if-the-Tree-is-a-MinHeap-2

Given Tree is not min-heap

We observe that at level 0, 30 > 22, and hence min-heap property is not satisfied

To verify the heap property, we ensure that each non-leaf node (parent) is smaller than its children. For every parent at index i, we compare it with its left child at 2*i + 1 and, if present, its right child at 2*i + 2. If a parent has only one child, we check against the left child alone.

C++
#include <bits/stdc++.h>
using namespace std;

// Checks if the given level order traversal represents a Min Heap
bool isMinHeap(vector<int> level) {   
    int n = level.size();

    // Verify each parent node is smaller than its children
    for (int i = (n / 2 - 1); i >= 0; i--) {
        if (level[i] > level[2 * i + 1])
            return false;
        if (2 * i + 2 < n && level[i] > level[2 * i + 2])
            return false;
    }
    return true;
}

// Driver code
int main() {
    vector<int> level = {10, 15, 14, 25, 30};
    cout << (isMinHeap(level) ? "True" : "False");
    return 0;
}
Java
// Java program to check if a given tree is a Min Heap
public class GfG {
    
    // Checks if the given level order traversal represents a Min Heap
    static boolean isMinHeap(int[] level) {
        int n = level.length - 1;

        // Verify each parent node is smaller than its children
        for (int i = (n / 2 - 1); i >= 0; i--) {
            if (level[i] > level[2 * i + 1])
                return false;
            if (2 * i + 2 < n && level[i] > level[2 * i + 2])
                return false;
        }
        return true;
    }

    // Driver code
    public static void main(String[] args) {
        int[] level = {10, 15, 14, 25, 30};
        System.out.println(isMinHeap(level) ? "True" : "False");
    }
}
Python
# Checks if the given level order traversal represents a Min Heap
def isMinHeap(level):
    n = len(level)
    
    # Verify each parent node is smaller than its children
    for i in range((n // 2) - 1, -1, -1):
        if level[i] > level[2 * i + 1]: 
            return False
        if 2 * i + 2 < n and level[i] > level[2 * i + 2]:
            return False
    return True

# Driver code
if __name__ == '__main__':
    level = [10, 15, 14, 25, 30] 
    print("True" if isMinHeap(level) else "False")
C#
using System;

class GfG
{
    // Checks if the given level order traversal represents a Min Heap
    public static bool isMinHeap(int[] level)
    {
        int n = level.Length - 1;

        // Verify each parent node is smaller than its children
        for (int i = (n / 2 - 1); i >= 0; i--)
        {
            if (level[i] > level[2 * i + 1])
            {
                return false;
            }

            if (2 * i + 2 < n && level[i] > level[2 * i + 2])
            {
                return false;
            }
        }
        return true;
    }

    public static void Main(string[] args)
    {
        int[] level = { 10, 15, 14, 25, 30 };
        Console.WriteLine(isMinHeap(level) ? "True" : "False");
    }
}
JavaScript
// Checks if the given level order traversal represents a Min Heap
function isMinHeap(level) {
    var n = level.length - 1;

    // Check if each parent node is smaller than its children
    for (var i = (n / 2 - 1); i >= 0; i--) {
        if (level[i] > level[2 * i + 1]) {
            return false;
        }
        if (2 * i + 2 < n && level[i] > level[2 * i + 2]) {
            return false;
        }
    }
    return true;
}

// Driver code
var level = [10, 15, 14, 25, 30];
console.log(isMinHeap(level) ? "True" : "False");

Output
True



Next Article
Article Tags :
Practice Tags :

Similar Reads