Add One Row to Tree

Imagine you're working on a tree visualization tool and need to expand your binary tree by adding a complete row of nodes at a specific depth. This is exactly what this problem asks you to do!

Given the root of a binary tree and two integers val and depth, you need to add a row of nodes with value val at the given depth. The root node is considered to be at depth 1.

The insertion rules are:
• For each non-null node at depth depth - 1, create two new nodes with value val
• The new left node becomes the current node's new left child
• The new right node becomes the current node's new right child
• The original left subtree becomes the new left node's left subtree
• The original right subtree becomes the new right node's right subtree
Special case: If depth == 1, create a new root with value val and make the original tree its left subtree

Return the root of the modified tree.

Input & Output

example_1.py — Basic insertion at depth 2
$ Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
💡 Note: We insert two nodes with value 1 at depth 2. The node with value 4 gets two new children (both with value 1). The original left subtree [2,3,1] becomes the left subtree of the new left child, and the original right subtree [6,5] becomes the right subtree of the new right child.
example_2.py — Insertion at root level
$ Input: root = [4,2,null,3,1], val = 1, depth = 1
Output: [1,4,null,2,null,3,1]
💡 Note: When depth = 1, we create a new root with value 1 and make the original tree its left subtree. The original root [4,2,null,3,1] becomes the left child of the new root.
example_3.py — Deep insertion
$ Input: root = [4,2,6,3,1,5], val = 1, depth = 3
Output: [4,2,6,1,1,1,1,3,null,null,1,null,null,null,5]
💡 Note: Insert at depth 3 means we add new nodes as children of all nodes at depth 2. Nodes 2 and 6 each get two new children with value 1, and their original children get connected appropriately.

Visualization

Tap to expand
Add One Row to Tree4Root (depth=1)26Target level (depth=2)NEW (val=1)NEW (val=1)New row inserted at depth=3315Original children reconnected
Understanding the Visualization
1
Identify Target Level
Find all nodes at depth-1 where new children will be added
2
Preserve Original Children
Store references to existing left and right subtrees
3
Insert New Nodes
Create two new nodes with target value as new children
4
Reconnect Subtrees
Attach original left subtree to new left child's left, and original right subtree to new right child's right
Key Takeaway
🎯 Key Insight: Track depth during traversal to identify exactly where to insert the new row, then carefully preserve parent-child relationships by connecting original subtrees to the appropriate sides of new nodes.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Visit each node exactly once during the traversal

n
2n
Linear Growth
Space Complexity
O(h)

Recursion stack space proportional to tree height h

n
2n
Linear Space

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • The depth of the tree is in the range [1, 104]
  • -100 ≤ Node.val ≤ 100
  • -105 ≤ val ≤ 105
  • 1 ≤ depth ≤ the depth of tree + 1
Asked in
Meta 45 Amazon 38 Microsoft 32 Google 28
89.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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