Complete Binary Tree Inserter - Problem

Imagine you're building a perfect pyramid structure where every level must be completely filled from left to right before starting the next level. This is exactly what a complete binary tree represents!

A complete binary tree is a binary tree where:

  • Every level is completely filled, except possibly the last level
  • The last level is filled from left to right
  • No gaps are allowed in any level

Your task is to design a CBTInserter class that maintains this perfect structure:

  • CBTInserter(TreeNode root): Initialize with an existing complete binary tree
  • int insert(int val): Insert a new node while keeping the tree complete, return the parent's value
  • TreeNode get_root(): Return the root of the tree

The challenge is to efficiently find the correct insertion position while maintaining the complete binary tree property!

Input & Output

example_1.py โ€” Basic Usage
$ Input: CBTInserter cbt = new CBTInserter([1,2]); cbt.insert(3); cbt.insert(4); cbt.get_root();
โ€บ Output: 2, 1, [1,2,3,4]
๐Ÿ’ก Note: First insert(3) attaches to node 2 (left child), returning 2. Second insert(4) attaches to node 2 (right child), returning 1 (next available parent). Final tree: 1 at root, with children 2,3 and 2 having children 4.
example_2.py โ€” Larger Tree
$ Input: CBTInserter cbt = new CBTInserter([1,2,3,4,5,6]); cbt.insert(7); cbt.insert(8);
โ€บ Output: 3, 4
๐Ÿ’ก Note: Tree starts complete to level 2: [1,2,3,4,5,6]. Insert(7) goes to node 3's left child, returning 3. Insert(8) goes to node 4's left child, returning 4. Maintains complete binary tree property.
example_3.py โ€” Single Node
$ Input: CBTInserter cbt = new CBTInserter([1]); cbt.insert(2); cbt.get_root();
โ€บ Output: 1, [1,2]
๐Ÿ’ก Note: Starting with single node 1, insert(2) adds as left child of 1, returning 1. Tree becomes [1,2] - still complete.

Constraints

  • The number of nodes in the tree is in the range [1, 1000]
  • Node values are in the range [0, 5000]
  • At most 104 calls will be made to insert and get_root

Visualization

Tap to expand
๐ŸŽญ Theater Seating System12345NEW๐ŸŽฏ Incomplete Row Queue345โ†’ Next available instantly!
Understanding the Visualization
1
Smart Ushering System
Instead of searching the entire theater each time, we keep a list of 'incomplete rows' that can still seat people
2
Instant Seating
When someone arrives, we immediately direct them to the first incomplete row without any searching
3
Dynamic Updates
When a row becomes full, we remove it from our list and add any new rows that can accept visitors
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining a queue of incomplete nodes, we achieve O(1) insertion time while perfectly preserving the complete binary tree structure!
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 25
28.7K Views
Medium Frequency
~18 min Avg. Time
956 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen