Check Completeness of a Binary Tree

Data Structures
Medium
Adobe
112.8K views

Given the root of a binary tree, determine if it is a complete binary tree. Use Breadth-First Search (BFS) for an efficient check.

Why Interviewers Ask This

Adobe evaluates this question to assess a candidate's ability to translate theoretical tree properties into efficient, production-ready code. They specifically look for mastery of Breadth-First Search (BFS) and the capacity to handle edge cases like null pointers or single-node trees without recursion overhead.

How to Answer This Question

1. Clarify the definition: Explicitly state that a complete binary tree must be filled level-by-level from left to right with no gaps until the last level. 2. Propose BFS Strategy: Suggest using a queue to traverse level-order, as this naturally exposes the structural order required. 3. Define the Stopping Condition: Explain the logic that once a null node is encountered, all subsequent nodes in the queue must also be null; otherwise, the tree is incomplete. 4. Handle Edge Cases: Mention checking for an empty root immediately before starting the traversal. 5. Complexity Analysis: Conclude by discussing time complexity O(N) and space complexity O(W), where W is the maximum width of the tree.

Key Points to Cover

  • Explicitly defining the 'no gaps' rule for the last level
  • Using a queue to enforce level-order traversal
  • Implementing the 'null flag' logic to detect gaps efficiently
  • Handling the empty tree edge case gracefully
  • Analyzing time and space complexity clearly

Sample Answer

To determine if a binary tree is complete, I would use a Breadth-First Search approach because it processes nodes level by level, which aligns perfectly with the definition of completeness. First, I'd initialize a queue and add the root. Then, I iterate through the queue. For each node, I check its children. If I encounter a null child, I set a flag indicating that we have reached the end of the tree structure. Crucially, once this flag is set, any subsequent non-null node found in the queue indicates a gap, meaning the tree is not complete. For example, consider a tree where the root has a left child but no right child, yet the left child has a right grandchild. In this case, after processing the root's missing right child, we hit our 'null' condition. However, when we later process the left child's right grandchild, we find a non-null node after the flag was triggered, correctly identifying the incompleteness. This method runs in O(N) time since we visit every node once, and uses O(W) space for the queue, where W is the maximum width. This solution is robust, avoids recursion depth issues, and handles the specific structural constraints Adobe values in their engineering roles.

Common Mistakes to Avoid

  • Attempting recursive solutions which complicate tracking the 'last level' state
  • Failing to check if subsequent nodes are non-null after encountering the first null
  • Ignoring the edge case where the root itself is null
  • Confusing complete binary trees with full binary trees in the explanation

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 25 Adobe questions