Add One Row to Tree
You are given the root of a binary tree. The root is at depth 1. Add a new row of nodes, all holding val, at depth depth, like this: for every node at depth depth - 1, create a new left child and a new right child holding val; the node's old left subtree becomes the left subtree of the new left child, and the old right subtree becomes the right subtree of the new right child. If depth is 1, the new node val becomes the root and the old tree is its left subtree. Return the root.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
Under 4 (depth 1) we add two new 1s; the old 2 hangs under the left 1 and the old 6 under the right 1.
Constraints
1 <= number of nodes <= 100-100 <= node.val, val <= 1001 <= depth <=(height of the tree)+ 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.