Check completeness
Challenge · Write code · about 12 min
Given the root of a binary tree, return true if it is complete: every level is full except maybe the last, and the last level is filled from the left with no gaps. Such a tree fits in an array with no empty slots.
Example 1
Input:
[[1,2,3,4,5,6]]
Output:
true
Slots 0 to 5 are filled with no gap.
Example 2
Input:
[[1,2,3,4,5,null,7]]
Output:
false
Slot 5 is empty, but 7 sits in slot 6.
Constraints
- 0 ≤ number of nodes ≤ 1,000
- -1,000 ≤ Node.val ≤ 1,000
- The tree is given in level order; null marks a missing child
Learn the idea first: Binary trees and how they are stored, a visual lesson.
More Binary trees and how they are stored problems
- Build from a level-order array Warm-up
- Children of index i Warm-up
- Which tree is complete? Standard
- Count the nodes Standard
- Sum of all values Standard