← Questions
Check whether a binary tree is complete
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Feb 2026. It tests trees and bfs.
Given the root of a binary tree, return whether it is complete. A complete binary tree has every level full except possibly the last, and the nodes of the last level are as far left as possible.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
is_complete_tree(root: TreeNode | None) -> bool— an empty tree is complete.
Example
Trees are shown in level order, with None for a missing child.
[1, 2, 3, 4, 5, 6] -> True
[1, 2, 3, 4, 5, None, 7] -> False # a gap before 7 on the last level
[1, 2, 3, 4] -> True
[1, 2, None, 4] -> False # level 2 is not full
Constraints
-
0 <= number of nodes <= 10^4.
Clarifying questions
- Is an empty tree complete?
- Is "complete" the same as "perfect" here, or may the last level be partial?
- Is the tree given as nodes or as a level-order array?
What a strong answer covers
- BFS that enqueues children even when they are null: once a null has been seen, any later non-null node means the tree is not complete.
- An alternative: number nodes as in a heap (root 1, children
2iand2i + 1); the tree is complete exactly when the largest index equals the node count. -
O(n)time andO(width)space. - Tests: empty tree, single node, a full last level, a last level filled from the left, a gap in the middle of the last level, a missing node on an earlier level.
Follow-ups
- Count the nodes of a complete tree in less than
O(n)time. - Check whether the tree is a valid max-heap (complete and heap-ordered).
- Check whether the tree is perfect, or full (every node has 0 or 2 children).
Where it's asked
- Meta · AI-enabled coding · Last asked Feb 202660 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
