Check a Complete Binary Tree
Problem statement
Given the root of a binary tree, return whether it is complete: every level before the last is full, and the nodes on the last level occupy the leftmost positions with no gap.
For this exercise, assume an empty tree is complete. Node values do not affect completeness. The input uses the conventional level-order tree serialization: null marks a missing child, redundant trailing nulls are ignored, and [] is the empty tree.
Interview context
The report tentatively compares the phone-screen task to a variant of checking a complete binary tree and says BFS was used. The exact variant was not retained. This practice task uses the conventional completeness definition above. Later discussion moved toward B-trees and database indexes; no specific B-tree operation is imposed on this binary-tree function.
Function
isCompleteTree(root: TreeNode) → booleanExamples
Example 1
root = [1,2,3,4,5,6]return = trueThe first two levels are full. The three nodes on the last level occupy its first three positions, so the tree is complete.
Example 2
root = [1,2,3,4,5,null,7]return = falseNode 3 has no left child but has right child 7. That occupied position follows a gap, so the tree is not complete.
Example 3
root = []return = trueThe empty tree is complete under the stated convention.
Constraints
- For this exercise, assume the serialized
rootcontains between0and2000entries after redundant trailing null markers are removed, including interior null markers. - Each non-null node value is an integer between
-1000000000and1000000000. - The serialization represents a valid finite binary tree; values may repeat.