root of a binary tree and two integers value and level. Levels are numbered from one: the root is on level 1, its children on level 2, and so on.
Insert a new level of nodes holding value so that it ends up exactly at level level:
- give every node on level level - 1 two new children holding value;
- its old left subtree becomes the left child of the new left node, and its old right subtree becomes the right child of the new right node;
- if level = 1, create a new root holding value and make the whole original tree its left subtree.
Return the root of the resulting tree.root=tree_node([5, 3, 8, None, 4]), value=0, level=2 → [5, 0, 0, 3, None, None, 8, None, 4]
Two new 0 nodes appear under the root 5. The old left subtree (3 with its child 4) hangs to the left of the left 0, and the old right subtree (8) to the right of the right 0.
root=tree_node([2, 1]), value=7, level=1 → [7, 2, None, 1]
With level = 1 the new node 7 becomes the root and the whole old tree becomes its left subtree.
root=tree_node([1, 2, 3]), value=9, level=3 → [1, 2, 3, 9, 9, 9, 9]
Level 3 is one below the deepest level, so every leaf gets two children with value 9.
Node.val ≤ 100root=tree_node([5, 3, 8, None, 4]), value=0, level=2
[5, 0, 0, 3, None, None, 8, None, 4]
Run checks the first 3 tests in your browser. Submit checks all 36 tests on the server, hidden ones included.