26. Is Binary Tree Heap

The problem can be found at the following link: 🔗 Question Link

🧩 Problem Description

Given a binary tree, determine whether it satisfies the properties of a Max-Heap.

A binary tree is a Max-Heap if:

  1. Completeness: All levels of the tree are completely filled except possibly the last, which is filled from left to right.

  2. Heap Property: The value of every node is greater than or equal to the values of its children.

📘 Examples

Example 1:

Input:

root[] = [97, 46, 37, 12, 3, 7, 31, 6, 9]

Output:

true

Explanation:

All levels are completely filled and the parent is always greater than or equal to children.

Example 2:

Input:

root[] = [97, 46, 37, 12, 3, 7, 31, N, 2, 4]

Output:

false

Explanation:

The tree is not complete and violates the max-heap property.

🔒 Constraints

  • $1 \leq \text{Number of Nodes} \leq 10^3$

  • $1 \leq \text{node->data} \leq 10^3$

✅ My Approach:

Recursive Count + Position Check

This approach combines:

  • Counting the number of nodes in the tree.

  • Verifying that the binary tree is complete and satisfies heap order property using a recursive index simulation.

Algorithm Steps:

  1. Recursively count the total number of nodes n in the tree.

  2. Traverse the tree with a recursive function using the theoretical array index of a complete binary tree (i):

    • If any node is placed at an index i >= n, then it's not complete.

    • Also, check if each node’s value is greater than its children’s.

  3. If all checks pass, return true.

🧮 Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), where n is the number of nodes in the binary tree, as each node is visited once.

  • Expected Auxiliary Space Complexity: O(h), where h is the height of the binary tree due to recursion stack usage.

💡 Code (C)

🧠 Code (C++)

⚡ Alternative Approaches

📊 2️⃣ Level Order + Count + Index Simulation

Algorithm Steps:

  1. Traverse the tree using level order traversal while assigning theoretical indices based on complete binary tree position (like heap array representation).

  2. If any node is found at index i where i ≥ n, the tree is not complete.

  3. At the same time, check whether each parent node is greater than its children (heap condition).

Why This Approach?

  • It directly simulates how a heap would be structured in an array.

  • Checks both completeness and heap properties together.

📝 Complexity Analysis:

  • Expected Time Complexity: O(n)

  • Expected Auxiliary Space Complexity: O(h), where h is height of tree (for recursion stack)

📊 3️⃣ Level Order Traversal + Flags

Algorithm Steps:

  1. Use a queue to perform a level-order traversal.

  2. Once a NULL child is found, set a flag.

  3. After this point, if any non-NULL node is found → Not Complete.

  4. Simultaneously check heap property: every node ≥ children.

Why This Approach?

  • Efficiently checks completeness and heap property in a single pass.

  • Uses a queue for level-order traversal, intuitive for BFS-minded devs.

📝 Complexity Analysis:

  • Expected Time Complexity: O(n)

  • Expected Auxiliary Space Complexity: O(n) due to queue usage

🆚 Comparison of Approaches

Approach

⏱️ Time

🗂️ Space

Pros

⚠️ Cons

Recursive index & count (main)

🟢 O(n)

🟢 O(h)

Elegant and clean recursion

Requires understanding index logic

Level Order + Index Simulation

🟢 O(n)

🟢 O(h)

Direct simulation of heap index

Slightly more complex implementation

Level Order Traversal + Flags

🟢 O(n)

🔴 O(n)

Very intuitive BFS-based logic

Queue space may grow for wide trees

Best Choice?

Scenario

Recommended Approach

✅ Concise and recursive

🥇 Recursive Index & Count

✅ Array index-based reasoning

🥈 Level Order + Index Simulation

✅ Prefer using queues and level traversal

🥉 Level Order + Flags

🧑‍💻 Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let’s make this learning journey more collaborative!

If you find this helpful, please give this repository a star!


📍Visitor Count

Last updated