Advertisement

Check Completeness of a Binary Tree - LeetCode 958 Solution

Check Completeness of a Binary Tree - Complete Solution Guide

Check Completeness of a Binary Tree is LeetCode problem 958, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree, determine if it is a complete binary tree . In a complete binary tree , every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2 h nodes inclusive at the last level h . Example 1: Input: root = [1,2,3,4,5,6] Output: true Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far lef

Detailed Explanation

The problem asks us to determine if a given binary tree is a 'complete binary tree'. A complete binary tree is one where all levels are completely filled except possibly the last level, and all nodes in the last level are as far left as possible. This means there should be no gaps in the tree when read level by level from left to right. The input is the root node of the binary tree, and the output is a boolean value: `true` if the tree is complete, and `false` otherwise. The constraints specify that the tree has between 1 and 100 nodes, and node values are between 1 and 1000.

Solution Approach

The solution uses a Breadth-First Search (BFS) algorithm implemented using a queue. The algorithm traverses the tree level by level. A boolean variable `found_null` is used to track whether a `null` node has been encountered. If we encounter a node after already having found a `null` node, then the tree is not a complete binary tree, and we return `false`. If we reach the end of the traversal without finding any non-null nodes after encountering a null node, then the tree is complete, and we return `true`.

Step-by-Step Algorithm

  1. Step 1: Initialize a queue and enqueue the root node.
  2. Step 2: Initialize a boolean variable `found_null` to `false`.
  3. Step 3: While the queue is not empty:
  4. Step 4: Dequeue a node from the queue.
  5. Step 5: If the dequeued node is `null`:
  6. Step 6: Set `found_null` to `true`.
  7. Step 7: Else (the dequeued node is not `null`):
  8. Step 8: If `found_null` is `true`:
  9. Step 9: Return `false` (because we found a non-null node after a null node).
  10. Step 10: Enqueue the left child of the dequeued node.
  11. Step 11: Enqueue the right child of the dequeued node.
  12. Step 12: After the queue is empty, return `true` (because we did not find any non-null nodes after a null node).

Key Insights

  • Insight 1: A complete binary tree can be validated by performing a level-order traversal (Breadth-First Search).
  • Insight 2: The key to identifying incompleteness is detecting a `null` node before all nodes on the last level have been visited. If we encounter a `null` node, any subsequent non-null nodes imply the tree is not complete.
  • Insight 3: We can use a queue to efficiently manage the level-order traversal.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Tree, Breadth-First Search, Binary Tree.

Companies

Asked at: Lyft.