Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.
Note that the root node is at depth 1.
The adding rule is:
depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.cur's original left subtree should be the left subtree of the new left subtree root.cur's original right subtree should be the right subtree of the new right subtree root.depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
Example 1:
Input: root = [4,2,6,3,1,5], val = 1, depth = 2 Output: [4,1,1,2,null,null,6,3,1,5]
Example 2:
Input: root = [4,2,null,3,1], val = 1, depth = 3 Output: [4,2,null,1,1,3,null,null,1]
Constraints:
[1, 104].[1, 104].-100 <= Node.val <= 100-105 <= val <= 1051 <= depth <= the depth of tree + 1Problem Overview: You are given the root of a binary tree, a value val, and a depth d. Insert a new row of nodes with value val at depth d. For every node currently at depth d-1, create two new nodes and attach the original children under them. If d = 1, the new node becomes the root and the existing tree becomes its left child.
Approach 1: Recursive Depth-First Traversal (O(n) time, O(h) space)
Use recursion to traverse the binary tree while tracking the current depth. When you reach nodes at depth d-1, create two new nodes with value val. The original left subtree becomes the left child of the new left node, and the original right subtree becomes the right child of the new right node. This works well because DFS naturally explores the structure while keeping the recursion stack limited to the tree height h. Space complexity is O(h) due to the recursion stack.
Approach 2: Iterative Breadth-First Traversal (O(n) time, O(w) space)
Perform a level-order traversal using a queue, a classic Breadth-First Search. Push the root into the queue and iterate level by level until you reach depth d-1. At that level, process each node in the queue: create two new nodes with value val, connect them as the node's new children, and reattach the original children beneath them. BFS is intuitive here because you directly move level by level, making it easy to stop exactly at the target depth.
Approach 3: Level Order BFS with Early Stop (O(n) time, O(w) space)
This variation of tree traversal uses the same queue-based level-order strategy but stops traversal once depth d-1 is reached. Instead of continuing to explore the rest of the tree, you only modify nodes in the queue at that level. This avoids unnecessary traversal and keeps the implementation clean. Space complexity is proportional to the maximum width w of the tree because the queue stores nodes from one level at a time.
Recommended for interviews: Both DFS and BFS run in O(n) time because every node may be visited once. Interviewers often prefer the BFS approach since it maps directly to the problem's “insert at depth” requirement. The recursive DFS solution demonstrates strong understanding of Depth-First Search and tree manipulation, which also makes it a solid interview answer. Showing both approaches proves you understand traversal tradeoffs.
This approach uses recursion to traverse the tree to the right depth, modifying the tree structure as necessary to add the new row of nodes. We'll use a recursive helper function that takes the current node and the current depth as parameters. If the current depth is at depth - 1, we add new nodes with the given value as left and right children. If the current depth is less, we recursively call the helper function on the left and right children (if they exist).
The base case for this recursion is reaching a null node, where we simply return.
This solution defines a TreeNode class for the tree nodes and a function addOneRow which is designed to modify the tree in place. It first checks if the depth is 1, which requires creating a new root node with the provided val and making the existing tree its left subtree. For deeper levels, it recursively traverses the tree using depth-first search, adding new nodes at the correct level.
Python
Time Complexity: O(n), because each node is visited exactly once.
Space Complexity: O(d), where d is the depth of the tree, due to the recursion stack.
This approach uses a breadth-first search (BFS) iteration over the tree. We can use a queue to traverse the tree level by level. At each level, if the current level is depth - 1, we add a new row of nodes. Otherwise, we queue the next level of nodes.
The solution involves creating a TreeNode class and a method that adds a row to the tree. If the depth is 1, a new root node is created with the current tree as its left child. For other depths, a queue is used to perform a level-order traversal until the level before the intended depth is reached. Then, new nodes with the given value are inserted as left and right children of the current nodes. The original children are reattached to the new nodes.
Java
Time Complexity: O(n), since every node is considered once in the BFS loop.
Space Complexity: O(n), where n is the number of nodes at the breadth of the tree, due to the queue.
This approach involves using a queue to perform a level-order traversal of the tree. The objective is to reach nodes at depth depth-1 such that we can add new nodes with value val at the desired depth.
If the depth is 1, simply create a new root node with value val and make the original tree the left subtree of the new root.
In this Python solution, a queue is used for breadth-first search (BFS) traversal. When the current depth equals depth-1, new nodes are inserted between the current nodes and their children, effectively at the specified depth. If the depth is 1, a new root is created with the existing root as its left child.
Time Complexity: O(n), where n is the number of nodes in the tree, as each node is visited once.
Space Complexity: O(n) in the worst case to keep the queue, if the tree is a balanced tree, or O(h) where h is the height of the tree in a skewed tree.
This approach uses a recursive depth-first search to traverse the tree. The function is designed to target depth depth-1 to manipulate and add the new nodes at the desired level.
An edge case at depth 1 involves creating a new root, similar to the BFS approach.
The C++ solution employs recursion to explore each depth level. The recursion continues to descend until reaching depth-1, where new nodes are created and attached. The solution gracefully handles the edge case of depth being 1 by creating a new root node.
Time Complexity: O(n), where n is the total number of nodes in the tree, as each node is visited once.
Space Complexity: O(h) due to the recursive call stack, where h is the height of the tree.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Recursive Depth-First Traversal | Time Complexity: O(n), because each node is visited exactly once. |
| Iterative Breadth-First Traversal | Time Complexity: O(n), since every node is considered once in the BFS loop. |
| Breadth-First Search (Level Order Traversal) | Time Complexity: O(n), where n is the number of nodes in the tree, as each node is visited once. Space Complexity: O(n) in the worst case to keep the queue, if the tree is a balanced tree, or O(h) where h is the height of the tree in a skewed tree. |
| Depth-First Search (Recursive) | Time Complexity: O(n), where n is the total number of nodes in the tree, as each node is visited once. Space Complexity: O(h) due to the recursive call stack, where h is the height of the tree. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Depth-First Traversal | O(n) | O(h) | When recursion is preferred and tree height is manageable |
| Iterative Breadth-First Traversal | O(n) | O(w) | Best when you want direct control over levels in the tree |
| Level Order BFS with Early Stop | O(n) | O(w) | Clean solution when modifying nodes only at a specific depth |
Add One Row to Tree | Made Super Easy | Google | Leetcode 623 | codestorywithMIK • codestorywithMIK • 12,151 views views
Watch 9 more video solutions →Practice Add One Row to Tree with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor