Check if a Binary Tree is Heap
Pattern: Heap implementation
A binary tree is a max heap when it is complete and every parent is at least its children. Number the nodes as an array heap would (root 0, children 2i + 1 and 2i + 2): the tree is complete exactly when every number is below the node count. Check the order with one more traversal.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Given the root of a binary tree, return whether it is a max heap: complete (every level full except perhaps the
last, filled from the left) and every node at least as big as its children. The tests give the tree in level order with
null for missing children.
The catalogue has no practice link for this one.
Think first
- What two conditions make a tree a heap?
- How does an array heap number its nodes?
- How can numbering show a gap?
Hints
Brute force, and why it fails
Walk the tree level by level; after the first missing child, any further node means it is not complete. That is also O(n) and works too.
The key insight
Giving each node its array index turns completeness into a simple bound check, so two short recursions decide it.
Dry run
| count | 9 |
|---|---|
| complete | true |
Tree [97, 46, 37, 12, 3, 7, 31, 6, 9] in level order: 9 nodes numbered 0 to 8, all below 9, so it is complete.
The plan
n = count(root)
complete(node, i): null -> true; i >= n -> false; recurse with 2i + 1 and 2i + 2
ordered(node): each child exists only if child.val <= node.val; recurse
return complete(root, 0) and ordered(root)
Complexity
- Time
O(n)- Space
O(h) call stack
Three passes over the nodes; the recursion is as deep as the tree.
Edge cases
- One node: true.
- A right child with no left child: not complete.
- Equal parent and child values are allowed.
I've built my logic, show the code
Java
class Solution {
public boolean isHeap(TreeNode root) {
int n = count(root);
return complete(root, 0, n) && ordered(root);
}
private int count(TreeNode node) {
return node == null ? 0 : 1 + count(node.left) + count(node.right);
}
private boolean complete(TreeNode node, int index, int n) { // number nodes as in an array heap
if (node == null) return true;
if (index >= n) return false; // a gap appeared before this node
return complete(node.left, 2 * index + 1, n) && complete(node.right, 2 * index + 2, n);
}
private boolean ordered(TreeNode node) { // every parent at least as big as its children
if (node == null) return true;
if (node.left != null && node.left.val > node.val) return false;
if (node.right != null && node.right.val > node.val) return false;
return ordered(node.left) && ordered(node.right);
}
}C++
class Solution {
public:
bool isHeap(TreeNode* root) {
int n = count(root);
return complete(root, 0, n) && ordered(root);
}
int count(TreeNode* node) {
return node == NULL ? 0 : 1 + count(node->left) + count(node->right);
}
bool complete(TreeNode* node, int index, int n) { // number nodes as in an array heap
if (node == NULL) return true;
if (index >= n) return false; // a gap appeared before this node
return complete(node->left, 2 * index + 1, n) && complete(node->right, 2 * index + 2, n);
}
bool ordered(TreeNode* node) { // every parent at least as big as its children
if (node == NULL) return true;
if (node->left != NULL && node->left->val > node->val) return false;
if (node->right != NULL && node->right->val > node->val) return false;
return ordered(node->left) && ordered(node->right);
}
};Python
class Solution:
def isHeap(self, root) -> bool:
n = self.count(root)
return self.complete(root, 0, n) and self.ordered(root)
def count(self, node) -> int:
return 0 if node is None else 1 + self.count(node.left) + self.count(node.right)
def complete(self, node, index: int, n: int) -> bool: # number nodes as in an array heap
if node is None:
return True
if index >= n:
return False # a gap appeared before this node
return self.complete(node.left, 2 * index + 1, n) and self.complete(node.right, 2 * index + 2, n)
def ordered(self, node) -> bool: # every parent at least as big as its children
if node is None:
return True
for child in (node.left, node.right):
if child is not None and child.val > node.val:
return False
return self.ordered(node.left) and self.ordered(node.right)Common mistakes
- Checking only the order, so an incomplete tree passes.
- Checking only parent and children values one level deep without recursing.