Check Completeness of a Binary Tree

Medium

This is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.

Question

Given the root of a binary tree, return whether it's complete.

A tree is complete when every level is full, except possibly the last one. The nodes on the last level must be as far left as possible, with no gaps between them.

Input: root = [64, 32, 96, 16, 48, 80]

Output: True

The first two levels are full. The last level has three nodes: 16, 48, and 80. They fill the leftmost spots with no gaps.

Input: root = [64, 32, 96, 16, None, 80, 112]

Output: False

32 is missing a right child. Its sibling 96 has two children after that gap on the same level.

Input: root = [64, None, 32]

Output: False

64's left slot is empty. Its right slot holds 32. That gap on the left means the tree isn't complete, even though it only has two nodes.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

Is this tree complete? If not, why not? root = [64, 32, 96, None, 48, 80, 112]
This tree, because 32 is missing a left child while 96 already has both of its children.
This tree, because the root has a value of 64 instead of 1.
This tree, because it only has 3 levels.
This tree is complete.

Remember to understand the problem and plan your approach before you start coding.