Skip to main content
LeetCode 958, Medium. Topics: Tree, Breadth-First Search, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 13 parametrized pytest cases, and a playground notebook:

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” 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
Example 2

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026