> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Check Completeness of a Binary Tree

> Tested Python solution for LeetCode 958 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 958, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Breadth-First Search](/catalog/topics/breadth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/check-completeness-of-a-binary-tree/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 958   # by problem number
lcpy gen -s check_completeness_of_a_binary_tree   # by problem name
```

## Problem

\<p>Given the \<code>root\</code> of a binary tree, determine if it is a \<em>complete binary tree\</em>.\</p>

\<p>In a \<strong>\<a href="[http://en.wikipedia.org/wiki/Binary\_tree#Types\_of\_binary\_trees](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)" target="\_blank">complete binary tree\</a>\</strong>, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between \<code>1\</code> and \<code>2\<sup>h\</sup>\</code> nodes inclusive at the last level \<code>h\</code>.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png)

```
Input: root = [1,2,3,4,5,6]
Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.
```

![Example 2](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png)

```
Input: root = [1,2,3,4,5,null,7]
Output: false
Explanation: The node with value 7 isn't as far left as possible.
```

### Constraints

* The number of nodes in the tree is in the range \[1, 100].
* 1 \<= Node.val \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_completeness_of_a_binary_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_completeness_of_a_binary_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque

from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(n)
    def is_complete_tree(self, root: TreeNode[int] | None) -> bool:
        if root is None:
            return True
        queue: deque[TreeNode[int] | None] = deque([root])
        seen_hole = False
        while queue:
            node = queue.popleft()
            if node is None:
                seen_hole = True
                continue
            if seen_hole:
                # A node appeared after a gap: not complete
                return False
            queue.append(node.left)
            queue.append(node.right)
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.