Opening the reading…
Opening the reading…
HEAP (PRIORITY QUEUE) › INTRODUCTORY QUESTIONS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A max-heap has two independent rules. The value rule is local: every existing child must be no larger than its parent. The shape rule is global: if you read positions from the root level by level, occupied positions must form one uninterrupted prefix. A tree can satisfy one rule and fail the other, so the solution must test both.
Breadth-first traversal visits exactly those positions from left to right. Number the root position 0, its children 1 and 2, and each later child using 2 * index + 1 or 2 * index + 2. Keep the next occupied position that a complete tree should provide. If a visited node has any other index, a position was skipped earlier, so completeness has already failed.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | Each node enters the queue once, leaves it once, and undergoes one index check plus at most two child comparisons. The traversal never returns to a processed node, so no node is counted twice. |
| Space | O(n) | The queue can hold an entire level, which is O(n) in the worst shape of the input. The stored index and a few local variables are constant extra space per queued node; the input tree and no returned collection are not counted as working output. |
#include <queue>
#include <utility>
using namespace std;
class Solution {
public:
bool isHeap(TreeNode* root) {
if (root == nullptr) {
return true;
}
queue<pair<TreeNode*, long long>> q;
q.push({root, 0});
long long nextIndex = 0;
while (!q.empty()) {
TreeNode* node = q.front().first;
long long index = q.front().second;
q.pop();
if (index != nextIndex) {
return false;
}
++nextIndex;
if (node->left != nullptr) {
if (node->left->val > node->val) {
return false;
}
q.push({node->left, 2 * index + 1});
}
if (node->right != nullptr) {
if (node->right->val > node->val) {
return false;
}
q.push({node->right, 2 * index + 2});
}
}
return true;
}
};The key placement is the index check before children are added. The queue can contain a node whose parent was valid, but that node may still reveal that an earlier position was skipped. Comparing its index with nextIndex catches that global shape error at the exact moment the invalid occupied position is reached.
You can validate completeness without carrying numeric positions. During breadth-first traversal, once a node is missing a child position, every later child position must also be missing. Record that event in a flag. If any later node has a child after the flag is set, reject the tree; heap order is checked at the same time.
#include <queue>
using namespace std;
class Solution {
public:
bool isHeap(TreeNode* root) {
if (root == nullptr) {
return true;
}
queue<TreeNode*> q;
q.push(root);
bool seenMissingPosition = false;
while (!q.empty()) {
TreeNode* node = q.front();
q.pop();
if (node->left != nullptr) {
if (seenMissingPosition || node->left->val > node->val) {
return false;
}
q.push(node->left);
} else {
seenMissingPosition = true;
}
if (node->right != nullptr) {
if (seenMissingPosition || node->right->val > node->val) {
return false;
}
q.push(node->right);
} else {
seenMissingPosition = true;
}
}
return true;
}
};This is a different arrangement rather than an asymptotic optimisation. It uses the same O(n) time and O(n) worst-case queue space, while avoiding index arithmetic and making the left-to-right rule explicit. The indexed version is often easier to generalize to array positions; the flag version is shorter when the only question is whether a gap has appeared.