← Questions

Check completeness of a binary tree

0
MetaCodingLast asked Dec 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Dec 2025. It tests binary tree and bfs.

Given the root of a binary tree, return whether it is complete: every level is full except possibly the last, and the nodes on the last level are as far left as possible.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • class TreeNode: val: int; left, right: TreeNode | None
  • is_complete(root: TreeNode) -> bool.

Example

      1
     / \
    2   3
   / \ /
  4  5 6          ->  True    # the last level is filled from the left

      1
     / \
    2   3
   / \   \
  4  5    7       ->  False   # 3 has a right child but no left child

      1
     / \
    2   3
     \
      5           ->  False   # a gap before 5 on the last level

      1           ->  True

Constraints

  • 1 <= nodes <= 10^4; values are arbitrary and may repeat.

Clarifying questions

  • Is a perfect tree complete? (Yes.)
  • Is a single node complete?
  • Are node values relevant? (No, only the shape.)
  • How deep can the tree be?

What a strong answer covers

  • Level-order traversal that enqueues children including None. Once a None has been seen, any later real node means the tree is not complete. O(n) time, O(width) space.
  • Why that is enough: in a complete tree, the level-order sequence is all nodes followed only by gaps.
  • The index alternative: number nodes as in a heap (root 1, children 2i and 2i + 1); the tree is complete exactly when the largest index equals the node count.
  • Tests: perfect tree, a missing left child, a gap in the middle of the last level, a single node.

Follow-ups

  • Count the nodes of a complete tree in less than O(n) (O(log² n) using heights).
  • Insert into a complete tree while keeping it complete.
  • Check whether the tree is a valid max-heap (complete and ordered).

Where it's asked

  • Meta · AI-enabled coding · Last asked Dec 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

binary tree · bfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding