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:
Completeness: All levels of the tree are completely filled except possibly the last, which is filled from left to right.
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:
Recursively count the total number of nodes
n
in the tree.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.
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)
int c(struct Node* r){return r?1+c(r->left)+c(r->right):0;}
int p(struct Node* r,int i,int n){
return !r
|| (i<n
&& (!r->left || r->key >= r->left->key)
&& (!r->right || r->key >= r->right->key)
&& p(r->left, 2*i+1, n)
&& p(r->right, 2*i+2, n)
);
}
bool isHeap(struct Node* root){
return p(root,0,c(root));
}
๐ง Code (C++)
class Solution {
int c(Node* r) { return r ? 1 + c(r->left) + c(r->right) : 0; }
bool p(Node* r, int i, int n) {
return !r
|| (i < n
&& (!r->left || r->data >= r->left->data)
&& (!r->right || r->data >= r->right->data)
&& p(r->left, 2*i+1, n)
&& p(r->right, 2*i+2, n)
);
}
public:
bool isHeap(Node* tree) {
return p(tree, 0, c(tree));
}
};
๐งโ๐ป Code (Java)
class Solution {
int c(Node r){return r==null?0:1+c(r.left)+c(r.right);}
boolean p(Node r,int i,int n){
return r==null
|| (i<n
&& (r.left ==null || r.data>=r.left.data)
&& (r.right ==null || r.data>=r.right.data)
&& p(r.left, 2*i+1, n)
&& p(r.right, 2*i+2, n)
);
}
boolean isHeap(Node tree){
return p(tree,0,c(tree));
}
}
๐ Code (Python)
class Solution:
def c(self, r):
return 1 + self.c(r.left) + self.c(r.right) if r else 0
def p(self, r, i, n):
return (not r) or (
i < n
and (not r.left or r.data >= r.left.data)
and (not r.right or r.data >= r.right.data)
and self.p(r.left, 2*i+1, n)
and self.p(r.right, 2*i+2, n)
)
def isHeap(self, root):
return self.p(root, 0, self.c(root))
๐ง 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