Advertisement

Complete Binary Tree Inserter - LeetCode 919 Solution

Complete Binary Tree Inserter - Complete Solution Guide

Complete Binary Tree Inserter is LeetCode problem 919, 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

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Design an algorithm to insert a new node to a complete binary tree keeping it complete after the insertion. Implement the CBTInserter class: CBTInserter(TreeNode root) Initializes the data structure with the root of the complete binary tree. int insert(int v) Inserts a TreeNode into the tree with value Node.val == val so that the tree remains co

Detailed Explanation

The problem asks us to design a `CBTInserter` class that can insert new nodes into a complete binary tree (CBT) while maintaining its completeness. A CBT is a binary tree where every level, except possibly the last, is completely filled, and all nodes are as far left as possible. The class needs to support three operations: 1. `CBTInserter(TreeNode root)`: Initializes the class with the root of an existing CBT. 2. `int insert(int val)`: Inserts a new node with the given value (`val`) into the CBT, making sure it remains complete. It returns the value of the parent node of the inserted node. 3. `TreeNode get_root()`: Returns the root node of the CBT.

Solution Approach

The solution uses a Breadth-First Search (BFS) during the `CBTInserter` initialization to identify nodes that are either missing a left child or missing both left and right children. These nodes are stored in a deque. The `insert` method then uses the first node in the deque as the parent, inserts the new node as either the left or right child depending on availability, updates the deque, and returns the parent's value. The `get_root` method simply returns the root of the tree.

Step-by-Step Algorithm

  1. Step 1: **Initialization:** Perform a BFS traversal of the tree using a queue. For each node encountered, check if it has a missing left or right child. If it does, add it to the deque of available parents.
  2. Step 2: **Insert:** When inserting a new node with value `val`, retrieve the first node from the deque (this is the parent).
  3. Step 3: **Connect New Node:** Create a new TreeNode with the given value. If the parent node has no left child, add the new node as its left child. Otherwise (parent has a left child but no right child), add the new node as the parent's right child. In this case, remove the parent from the head of deque since it is now full.
  4. Step 4: **Update Deque:** Add the newly inserted node to the end of the deque. This ensures the newly inserted node is considered as a possible parent for future insertions.
  5. Step 5: **Return Parent Value:** Return the value of the parent node.
  6. Step 6: **Get Root:** The get_root function simply returns the root of the tree.

Key Insights

  • Insight 1: We need to efficiently find the parent node where the new node should be inserted to maintain the complete binary tree property.
  • Insight 2: A Breadth-First Search (BFS) during initialization helps us identify the nodes that are missing left or right children. A queue is a suitable data structure for BFS.
  • Insight 3: Using a deque to store the nodes that can still accept children allows for O(1) access to potential parent nodes.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(W)

Topics

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

Companies

Asked at: PhonePe.