Check Completeness of a Binary Tree

Given the root of a binary tree, return true if it is a complete binary tree. In a complete binary tree every level except possibly the last is completely filled, and all nodes in the last level are as far left as possible.

30:00

Check Completeness of a Binary Tree
medium
Free written solution
Topics
Companies

Given the root of a binary tree, return true if it is a complete binary tree. In a complete binary tree every level except possibly the last is completely filled, and all nodes in the last level are as far left as possible.

Example 1:
Input: {"root":[1,2,3,4,5,6]}
Output: true
Constraints:
  • The number of nodes in the tree is in the range [1,100][1, 100].

  • 1≤Node.val≤10001 \leq \text{Node.val} \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[1,2,3,4,5,6]}

Put the root 1 on the queue. A BFS visits slots in level order, so a complete tree shows every node before any empty slot.

123456

slots in BFS order (no gap yet)

nothing dequeued yet

queue (front first)

1
seen_null = False

checked 0 slots

dequeued now
in queue
filled slot
first gap / node after gap
Variables
VariableValue
node-
seen_nullFalse
queue[1]
slot0
DepthFunction Call
Stack empty
0/14