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.

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

Visualization

Tap to expand
Add One Row to Tree INPUT Original Tree: 4 2 6 3 1 5 d=1 d=2 d=3 root = [4,2,6,3,1,5] val = 1, depth = 2 Insert row at depth 2 Array representation: 4 2 6 3 1 5 ALGORITHM (DFS) 1 Check depth If depth=1, new root else traverse to d-1 2 DFS to depth-1 Find nodes at d=1 (parent level) 3 Insert new nodes Create left & right nodes with val=1 4 Reconnect subtrees Old left --> new left Old right --> new right At node 4 (depth 1): 4 1 1 2 6 NEW old FINAL RESULT Modified Tree: 4 1 1 2 6 3 1 5 d=1 d=2 NEW d=3 d=4 Output: [4,1,1,2,null,null,6,3,1,5] OK - Row Added at d=2 Result array: 4 1 1 2 - - 6 3 1 5 Key Insight: Use DFS to reach depth-1 level, then insert new nodes between parent and children. The new nodes act as "middlemen" - they become children of the parent and parents of the original subtrees. Time: O(n), Space: O(h) where h is tree height. TutorialsPoint - Add One Row to Tree | DFS Approach
Asked in
Meta 45 Amazon 38 Microsoft 32 Google 28
89.4K 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