Check Completeness of a Binary Tree

MediumTreesbfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A binary tree is **complete** when every level except possibly the last is completely filled, and all nodes on the last level are packed as far **left** as possible (no gap may appear to the left of any node on that level). Given the root of a non-empty binary tree, return true if it is complete and false otherwise. Equivalently: in a level-order traversal that also enqueues missing children, no real node may appear after the first missing one. That check runs in O(n).

Examples

Input: root = [1,2,3,4,5,6]
Output: true
The first two levels are full and the last level 4,5,6 is packed to the left.
Input: root = [1,2,3,4,5,null,7]
Output: false
Node 3 is missing its left child but has a right child 7, leaving a gap.

Constraints

  • 1 <= number of nodes <= 100
  • 1 <= Node.val <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bfs problems