Skip to content

Commit efd1ac6

Browse files
authored
feat: add js solution to lc problem: No.1110 (doocs#3284)
1 parent b711bea commit efd1ac6

File tree

5 files changed

+497
-0
lines changed

5 files changed

+497
-0
lines changed

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

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,180 @@ 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+
347+
<!-- tabs:end -->
348+
349+
<!-- solution:end -->
350+
351+
<!-- solution:start -->
352+
353+
### 方法二:BFS
354+
355+
<!-- tabs:start -->
356+
357+
#### TypeScript
358+
359+
```ts
360+
/**
361+
* Definition for a binary tree node.
362+
* class TreeNode {
363+
* val: number
364+
* left: TreeNode | null
365+
* right: TreeNode | null
366+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
367+
* this.val = (val===undefined ? 0 : val)
368+
* this.left = (left===undefined ? null : left)
369+
* this.right = (right===undefined ? null : right)
370+
* }
371+
* }
372+
*/
373+
export function delNodes(root: T, to_delete: number[]): Array<T> {
374+
if (!root) return [];
375+
376+
const del = new Set(to_delete);
377+
const res: T[] = [];
378+
let q: TreeNode[] = [root];
379+
380+
while (q.length) {
381+
const qNext: TreeNode[] = [];
382+
383+
for (const node of q) {
384+
if (node.left) {
385+
qNext.push(node.left);
386+
387+
if (del.has(node.left.val)) {
388+
node.left = null;
389+
}
390+
}
391+
392+
if (node.right) {
393+
qNext.push(node.right);
394+
395+
if (del.has(node.right.val)) {
396+
node.right = null;
397+
}
398+
}
399+
400+
if (del.has(node.val)) {
401+
if (node.left) res.push(node.left);
402+
if (node.right) res.push(node.right);
403+
}
404+
}
405+
406+
q = qNext;
407+
}
408+
409+
if (!del.has(root.val)) res.push(root);
410+
411+
return res;
412+
}
413+
414+
type T = TreeNode | null;
415+
```
416+
417+
#### JavaScript
418+
419+
```js
420+
/**
421+
* Definition for a binary tree node.
422+
* function TreeNode(val, left, right) {
423+
* this.val = (val===undefined ? 0 : val)
424+
* this.left = (left===undefined ? null : left)
425+
* this.right = (right===undefined ? null : right)
426+
* }
427+
*/
428+
/**
429+
* @param {TreeNode} root
430+
* @param {number[]} to_delete
431+
* @return {TreeNode[]}
432+
*/
433+
var delNodes = function (root, to_delete) {
434+
if (!root) return [];
435+
436+
const del = new Set(to_delete);
437+
const res = [];
438+
let q = [root];
439+
440+
while (q.length) {
441+
const qNext = [];
442+
443+
for (const node of q) {
444+
if (node.left) {
445+
qNext.push(node.left);
446+
447+
if (del.has(node.left.val)) {
448+
node.left = null;
449+
}
450+
}
451+
452+
if (node.right) {
453+
qNext.push(node.right);
454+
455+
if (del.has(node.right.val)) {
456+
node.right = null;
457+
}
458+
}
459+
460+
if (del.has(node.val)) {
461+
if (node.left) res.push(node.left);
462+
if (node.right) res.push(node.right);
463+
}
464+
}
465+
466+
q = qNext;
467+
}
468+
469+
if (!del.has(root.val)) res.push(root);
470+
471+
return res;
472+
};
473+
```
474+
301475
<!-- tabs:end -->
302476

303477
<!-- solution:end -->

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

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,180 @@ 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+
343+
<!-- tabs:end -->
344+
345+
<!-- solution:end -->
346+
347+
<!-- solution:start -->
348+
349+
### Solution 2: BFS
350+
351+
<!-- tabs:start -->
352+
353+
#### TypeScript
354+
355+
```ts
356+
/**
357+
* Definition for a binary tree node.
358+
* class TreeNode {
359+
* val: number
360+
* left: TreeNode | null
361+
* right: TreeNode | null
362+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
363+
* this.val = (val===undefined ? 0 : val)
364+
* this.left = (left===undefined ? null : left)
365+
* this.right = (right===undefined ? null : right)
366+
* }
367+
* }
368+
*/
369+
export function delNodes(root: T, to_delete: number[]): Array<T> {
370+
if (!root) return [];
371+
372+
const del = new Set(to_delete);
373+
const res: T[] = [];
374+
let q: TreeNode[] = [root];
375+
376+
while (q.length) {
377+
const qNext: TreeNode[] = [];
378+
379+
for (const node of q) {
380+
if (node.left) {
381+
qNext.push(node.left);
382+
383+
if (del.has(node.left.val)) {
384+
node.left = null;
385+
}
386+
}
387+
388+
if (node.right) {
389+
qNext.push(node.right);
390+
391+
if (del.has(node.right.val)) {
392+
node.right = null;
393+
}
394+
}
395+
396+
if (del.has(node.val)) {
397+
if (node.left) res.push(node.left);
398+
if (node.right) res.push(node.right);
399+
}
400+
}
401+
402+
q = qNext;
403+
}
404+
405+
if (!del.has(root.val)) res.push(root);
406+
407+
return res;
408+
}
409+
410+
type T = TreeNode | null;
411+
```
412+
413+
#### JavaScript
414+
415+
```js
416+
/**
417+
* Definition for a binary tree node.
418+
* function TreeNode(val, left, right) {
419+
* this.val = (val===undefined ? 0 : val)
420+
* this.left = (left===undefined ? null : left)
421+
* this.right = (right===undefined ? null : right)
422+
* }
423+
*/
424+
/**
425+
* @param {TreeNode} root
426+
* @param {number[]} to_delete
427+
* @return {TreeNode[]}
428+
*/
429+
var delNodes = function (root, to_delete) {
430+
if (!root) return [];
431+
432+
const del = new Set(to_delete);
433+
const res = [];
434+
let q = [root];
435+
436+
while (q.length) {
437+
const qNext = [];
438+
439+
for (const node of q) {
440+
if (node.left) {
441+
qNext.push(node.left);
442+
443+
if (del.has(node.left.val)) {
444+
node.left = null;
445+
}
446+
}
447+
448+
if (node.right) {
449+
qNext.push(node.right);
450+
451+
if (del.has(node.right.val)) {
452+
node.right = null;
453+
}
454+
}
455+
456+
if (del.has(node.val)) {
457+
if (node.left) res.push(node.left);
458+
if (node.right) res.push(node.right);
459+
}
460+
}
461+
462+
q = qNext;
463+
}
464+
465+
if (!del.has(root.val)) res.push(root);
466+
467+
return res;
468+
};
469+
```
470+
297471
<!-- tabs:end -->
298472

299473
<!-- solution:end -->

0 commit comments

Comments
 (0)