Skip to content

Commit 3ebdabd

Browse files
committed
feat: add ts solution to lc problem: No.0623
No.0623.Add One Row to Tree
1 parent 7af3439 commit 3ebdabd

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

solution/0600-0699/0623.Add One Row to Tree/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,49 @@ func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
213213
}
214214
```
215215

216+
### **TypeScript**
217+
218+
```ts
219+
/**
220+
* Definition for a binary tree node.
221+
* class TreeNode {
222+
* val: number
223+
* left: TreeNode | null
224+
* right: TreeNode | null
225+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
226+
* this.val = (val===undefined ? 0 : val)
227+
* this.left = (left===undefined ? null : left)
228+
* this.right = (right===undefined ? null : right)
229+
* }
230+
* }
231+
*/
232+
233+
function addOneRow(
234+
root: TreeNode | null,
235+
val: number,
236+
depth: number,
237+
): TreeNode | null {
238+
if (depth === 1) {
239+
return new TreeNode(val, root);
240+
}
241+
242+
const queue = [root];
243+
for (let i = 1; i < depth - 1; i++) {
244+
const n = queue.length;
245+
for (let j = 0; j < n; j++) {
246+
const { left, right } = queue.shift();
247+
left && queue.push(left);
248+
right && queue.push(right);
249+
}
250+
}
251+
for (const node of queue) {
252+
node.left = new TreeNode(val, node.left);
253+
node.right = new TreeNode(val, null, node.right);
254+
}
255+
return root;
256+
}
257+
```
258+
216259
### **...**
217260

218261
```

solution/0600-0699/0623.Add One Row to Tree/README_EN.md

+43
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,49 @@ func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
200200
}
201201
```
202202

203+
### **TypeScript**
204+
205+
```ts
206+
/**
207+
* Definition for a binary tree node.
208+
* class TreeNode {
209+
* val: number
210+
* left: TreeNode | null
211+
* right: TreeNode | null
212+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
213+
* this.val = (val===undefined ? 0 : val)
214+
* this.left = (left===undefined ? null : left)
215+
* this.right = (right===undefined ? null : right)
216+
* }
217+
* }
218+
*/
219+
220+
function addOneRow(
221+
root: TreeNode | null,
222+
val: number,
223+
depth: number,
224+
): TreeNode | null {
225+
if (depth === 1) {
226+
return new TreeNode(val, root);
227+
}
228+
229+
const queue = [root];
230+
for (let i = 1; i < depth - 1; i++) {
231+
const n = queue.length;
232+
for (let j = 0; j < n; j++) {
233+
const { left, right } = queue.shift();
234+
left && queue.push(left);
235+
right && queue.push(right);
236+
}
237+
}
238+
for (const node of queue) {
239+
node.left = new TreeNode(val, node.left);
240+
node.right = new TreeNode(val, null, node.right);
241+
}
242+
return root;
243+
}
244+
```
245+
203246
### **...**
204247

205248
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function addOneRow(
16+
root: TreeNode | null,
17+
val: number,
18+
depth: number,
19+
): TreeNode | null {
20+
if (depth === 1) {
21+
return new TreeNode(val, root);
22+
}
23+
24+
const queue = [root];
25+
for (let i = 1; i < depth - 1; i++) {
26+
const n = queue.length;
27+
for (let j = 0; j < n; j++) {
28+
const { left, right } = queue.shift();
29+
left && queue.push(left);
30+
right && queue.push(right);
31+
}
32+
}
33+
for (const node of queue) {
34+
node.left = new TreeNode(val, node.left);
35+
node.right = new TreeNode(val, null, node.right);
36+
}
37+
return root;
38+
}

0 commit comments

Comments
 (0)