File tree Expand file tree Collapse file tree 3 files changed +169
-0
lines changed
solution/1100-1199/1110.Delete Nodes And Return Forest Expand file tree Collapse file tree 3 files changed +169
-0
lines changed Original file line number Diff line number Diff line change @@ -414,6 +414,64 @@ export function delNodes(root: T, to_delete: number[]): Array<T> {
414
414
type T = TreeNode | null ;
415
415
```
416
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
+
417
475
<!-- tabs: end -->
418
476
419
477
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -410,6 +410,64 @@ export function delNodes(root: T, to_delete: number[]): Array<T> {
410
410
type T = TreeNode | null ;
411
411
```
412
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
+
413
471
<!-- tabs: end -->
414
472
415
473
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ if ( ! root ) return [ ] ;
16
+
17
+ const del = new Set ( to_delete ) ;
18
+ const res = [ ] ;
19
+ let q = [ root ] ;
20
+
21
+ while ( q . length ) {
22
+ const qNext = [ ] ;
23
+
24
+ for ( const node of q ) {
25
+ if ( node . left ) {
26
+ qNext . push ( node . left ) ;
27
+
28
+ if ( del . has ( node . left . val ) ) {
29
+ node . left = null ;
30
+ }
31
+ }
32
+
33
+ if ( node . right ) {
34
+ qNext . push ( node . right ) ;
35
+
36
+ if ( del . has ( node . right . val ) ) {
37
+ node . right = null ;
38
+ }
39
+ }
40
+
41
+ if ( del . has ( node . val ) ) {
42
+ if ( node . left ) res . push ( node . left ) ;
43
+ if ( node . right ) res . push ( node . right ) ;
44
+ }
45
+ }
46
+
47
+ q = qNext ;
48
+ }
49
+
50
+ if ( ! del . has ( root . val ) ) res . push ( root ) ;
51
+
52
+ return res ;
53
+ } ;
You can’t perform that action at this time.
0 commit comments