Check Completeness of a Binary Tree
MediumThis 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
Remember to understand the problem and plan your approach before you start coding.