Okay, it is binary tree again. Taking recursion as the first approach, it is very easy to override the original function signature by adding a parameter side
defining whether the current node should be extended in left or right (i.e., 0 => left, 1 => right). The rest is trivial.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function addOneRow(root: TreeNode | null, val: number, depth: number, side: number = 0): TreeNode | null {
if ( depth === 1 ) {
if ( side === 0 ) {
return new TreeNode( val, root, null )
} else {
return new TreeNode( val, null, root )
}
} else if ( depth < 1) {
return root
}
if ( root === null ) return null;
return new TreeNode(
root.val,
addOneRow( root.left, val, depth - 1, 0 ),
addOneRow( root.right, val, depth - 1, 1 )
)
};