Skip to content

Commit 9a74e80

Browse files
committed
feat: add js solution to lc problem: No.1110
1 parent b711bea commit 9a74e80

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

solution/1100-1199/1110.Delete Nodes And Return Forest/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,52 @@ function delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode |
298298
}
299299
```
300300

301+
#### JavaScript
302+
303+
```js
304+
/**
305+
* Definition for a binary tree node.
306+
* function TreeNode(val, left, right) {
307+
* this.val = (val===undefined ? 0 : val)
308+
* this.left = (left===undefined ? null : left)
309+
* this.right = (right===undefined ? null : right)
310+
* }
311+
*/
312+
/**
313+
* @param {TreeNode} root
314+
* @param {number[]} to_delete
315+
* @return {TreeNode[]}
316+
*/
317+
var delNodes = function (root, to_delete) {
318+
const s = Array(1001).fill(false);
319+
for (const x of to_delete) {
320+
s[x] = true;
321+
}
322+
const ans = [];
323+
const dfs = root => {
324+
if (!root) {
325+
return null;
326+
}
327+
root.left = dfs(root.left);
328+
root.right = dfs(root.right);
329+
if (!s[root.val]) {
330+
return root;
331+
}
332+
if (root.left) {
333+
ans.push(root.left);
334+
}
335+
if (root.right) {
336+
ans.push(root.right);
337+
}
338+
return null;
339+
};
340+
if (dfs(root)) {
341+
ans.push(root);
342+
}
343+
return ans;
344+
};
345+
```
346+
301347
<!-- tabs:end -->
302348

303349
<!-- solution:end -->

solution/1100-1199/1110.Delete Nodes And Return Forest/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,52 @@ function delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode |
294294
}
295295
```
296296

297+
#### JavaScript
298+
299+
```js
300+
/**
301+
* Definition for a binary tree node.
302+
* function TreeNode(val, left, right) {
303+
* this.val = (val===undefined ? 0 : val)
304+
* this.left = (left===undefined ? null : left)
305+
* this.right = (right===undefined ? null : right)
306+
* }
307+
*/
308+
/**
309+
* @param {TreeNode} root
310+
* @param {number[]} to_delete
311+
* @return {TreeNode[]}
312+
*/
313+
var delNodes = function (root, to_delete) {
314+
const s = Array(1001).fill(false);
315+
for (const x of to_delete) {
316+
s[x] = true;
317+
}
318+
const ans = [];
319+
const dfs = root => {
320+
if (!root) {
321+
return null;
322+
}
323+
root.left = dfs(root.left);
324+
root.right = dfs(root.right);
325+
if (!s[root.val]) {
326+
return root;
327+
}
328+
if (root.left) {
329+
ans.push(root.left);
330+
}
331+
if (root.right) {
332+
ans.push(root.right);
333+
}
334+
return null;
335+
};
336+
if (dfs(root)) {
337+
ans.push(root);
338+
}
339+
return ans;
340+
};
341+
```
342+
297343
<!-- tabs:end -->
298344

299345
<!-- solution:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number[]} to_delete
12+
* @return {TreeNode[]}
13+
*/
14+
var delNodes = function (root, to_delete) {
15+
const s = Array(1001).fill(false);
16+
for (const x of to_delete) {
17+
s[x] = true;
18+
}
19+
const ans = [];
20+
const dfs = root => {
21+
if (!root) {
22+
return null;
23+
}
24+
root.left = dfs(root.left);
25+
root.right = dfs(root.right);
26+
if (!s[root.val]) {
27+
return root;
28+
}
29+
if (root.left) {
30+
ans.push(root.left);
31+
}
32+
if (root.right) {
33+
ans.push(root.right);
34+
}
35+
return null;
36+
};
37+
if (dfs(root)) {
38+
ans.push(root);
39+
}
40+
return ans;
41+
};

0 commit comments

Comments
 (0)