Skip to content
Problem 623 · LeetCode 623(opens in a new tab)

Add One Row to Tree

MediumBreadth-first search

A binary tree is passed in as root, along with two whole numbers, val and depth. The root sits at depth 1, its children at depth 2, and so on. Insert a new row of nodes, all holding val, so that the new nodes sit at depth depth.

Every node on depth depth − 1 gets two new children. The new left child takes over that node's old left subtree as its own left subtree, and the new right child takes over the old right subtree as its own right subtree. If depth is 1, the new node becomes the root and the whole old tree becomes its left subtree. Return the root of the changed tree.

Example 1

Input:  root = [5, 8, 2, null, 3, 7], val = 0, depth = 3
Output: [5, 8, 2, 0, 0, 0, 0, null, null, null, 3, 7]
Why:    8 and 2 are at depth 2; each gets two 0s above its old children
2 more examples and the constraints

Example 2

Input:  root = [4, 6], val = 9, depth = 1
Output: [9, 4, null, 6]
Why:    9 becomes the root, with the old tree on its left

Example 3

Input:  root = [1, 2], val = 5, depth = 3
Output: [1, 2, null, 5, 5]
Why:    the new row goes below the deepest level

Constraints

  • 1 ≤ number of nodes ≤ 10,000, and the tree is at most 10,000 levels deep
  • −100 ≤ Node.val ≤ 100
  • −100,000 ≤ val ≤ 100,000
  • 1 ≤ depth ≤ the tree's depth + 1

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Recursive DFS to depth − 1O(n)
  2. Level-order walk with a queueO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 10,000 nodesVerdict
Recursive DFS to depth − 1O(n)
Passes; deep recursion on chains
Level-order walk with a queueO(n)
Best; no recursion

n = number of nodes, h = height, w = widest level. Both stop at depth − 1.

Edge cases to test

  • depth is 1. There is no level above; the new node becomes the root with the old tree on its left.
  • depth is one past the bottom. The new nodes become leaves under the deepest nodes; Example 3 shows it.
  • Missing children. A node with an empty side still gets a new node on that side, with nothing under it.
  • A long chain. With 10,000 levels, recursion can overflow; the queue version is safe.

Common mistakes

  • Putting the old right subtree under the new left node (or the other way round).
  • Forgetting the depth 1 case, where no node sits above the new row.
  • Walking one level too far, so the row lands at depth + 1.

How to talk through it

  1. 1Clarify. Is the root depth 1? What happens when depth is 1 or one past the bottom?
  2. 2Find the level. Only nodes on depth − 1 change; everything else stays put.
  3. 3Splice. New left takes the old left subtree on its left; new right takes the old right subtree on its right.
  4. 4Pick the walk. DFS with a depth counter or BFS level by level; BFS avoids deep recursion.
  5. 5Test out loud. Run depth 1, depth past the bottom, missing children and a long chain.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?