# 958. Check Completeness of a Binary Tree

检查二叉树是否complete。完全二叉树只有最后一层不完整，且最后一层只有右侧可能有连着的空缺。检查层信息，用BFS。完整二叉树seriliaze后中间不会出现null，利用这条性质在BFS时无论l和r是否为null都往q里插，当弹出遇到null后如果是complete的，BFS应该结束；否则不是complete的。

```cpp
/*
 * @lc app=leetcode id=958 lang=cpp
 *
 * [958] Check Completeness of a Binary Tree
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isCompleteTree(TreeNode* root) {
        if (root == nullptr) return false;
        queue<TreeNode*> q;
        q.push(root);
        bool end = false;
        while (!q.empty()) {
            const auto t = q.front();
            q.pop();
            if (t == nullptr) end = true;
            else {
                if (end) return false;
                q.push(t->left);
                q.push(t->right);
            }
        }
        return true;
    }
};
// @lc code=end


```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hao-fu-1.gitbook.io/oj/binary_tree_and_divide_conquer/order-traversal/958.-check-completeness-of-a-binary-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
