Problem 405
Check Completeness of a Binary Tree
Medium · 20 pts · Tree, Breadth-First Search, Binary Tree
Problem
A binary tree is complete if every level is completely filled, except possibly the last one, and the nodes of the last level are as far left as possible. Decide whether the given tree is complete. In a level-order traversal that also records missing children, a complete tree never has a real node after a missing one.
Input
The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.
Output
Print YES if the tree is complete, otherwise NO.
Constraints
1 <= number of nodes <= 100
Example input
6 1 2 3 4 5 6
Example output
YES
Explanation
In the tree 1 / 2 3 / 4 5 6, every level before the last is full and the last level 4 5 6 is packed to the left, so the tree is complete.