How to determine whether a binary tree is complete?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A binary tree is complete if every level is full except possibly the last, and the last level is filled from left to right with no gaps. The easiest way to check that property is to traverse the tree in level order and verify that once a missing child position appears, no later real node appears after it.
The Core Property
A complete binary tree allows missing nodes only at the far right end of the last level. That gives us a useful test:
- scan the tree level by level from left to right
- once you encounter a
Noneposition, every later position must also beNone
If a real node appears after a gap, the tree is not complete.
Breadth-First Search Solution
This rule maps directly to a queue-based breadth-first traversal.
This prints True for a complete tree.
Why The Algorithm Works
Level-order traversal visits nodes in the exact order that completeness is defined. If the tree is complete, all actual nodes occupy the leftmost available positions, so after the first empty slot there can be no later real node.
If a later real node does appear, the tree has a gap before a node on the same level or a lower level, which violates completeness.
Example Of An Incomplete Tree
Consider this structure:
- root has both children
- the left child is missing a left node but has a right node
That already breaks completeness because a node appears to the right of a gap.
This prints False.
Index-Based Alternative
Another common method numbers nodes as if the tree were stored in an array. In such a layout:
- root gets index
0 - left child gets
2 * i + 1 - right child gets
2 * i + 2
If the tree has n real nodes and is complete, the largest assigned index must be less than n.
This method is also correct, but it requires either two passes or a combined recursive structure. The queue method is often easier to explain in interviews.
Complexity
Both common approaches run in O(n) time because each node is visited a constant number of times. The queue-based approach uses O(n) space in the worst case for the traversal queue. The indexed recursive approach uses O(h) call stack space, where h is the tree height, plus any space used to count nodes.
Common Pitfalls
The most common mistake is checking only whether every node has either zero or two children. That tests for a full tree, not a complete tree, and it rejects many valid complete trees.
Another mistake is traversing depth-first and trying to infer left-to-right level constraints from local shape alone. Completeness is fundamentally a level-order property, so BFS is the safer direct method.
A third issue is mishandling the empty tree. By convention, an empty tree is complete.
Summary
- A complete binary tree can have missing nodes only at the far right of the last level.
- A level-order traversal with a gap flag gives a clean
O(n)solution. - Once a
Noneposition is seen, no later real node may appear. - The indexed-array method is a valid alternative with the same asymptotic time complexity.
- Do not confuse complete trees with full or perfect trees.
Related reading
- How to determine whether two circular sectors overlap with each other
- How to devise this solution to Non-Constructible Change challenge from Algoexpert.io
- How to disable sort in DataGridView?
- How to divide a set into two sets such that the difference of the average is minimum?
- How to directly initialize a HashMap (in a literal way)?
- How to display the value on horizontal bars
- How to divide number into integer pieces that are each a multiple of n?
- How to do a range update in Binary Indexed Tree or Fenwick Tree?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.