-
Notifications
You must be signed in to change notification settings - Fork 0
/
1110.delete-nodes-and-return-forest.js
138 lines (103 loc) · 3.4 KB
/
1110.delete-nodes-and-return-forest.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* @lc app=leetcode id=1110 lang=javascript
*
* [1110] Delete Nodes And Return Forest
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
// let roots = [];
// let search = function(root, isRoot){
// if(!root) return null;
// let shouldDelete = to_delete.includes(root.val);
// if(isRoot && !shouldDelete) roots.push(root);
// root.left = search(root.left, shouldDelete);
// root.right = search(root.right, shouldDelete);
// return shouldDelete ? null:root;
// }
// search(root, true);
// return roots;
var delNodes = function(root, to_delete) {
let head = root;
const output = []
let deleteNums = new Set(to_delete)
const dfs = (node, isRoot) => {
if(!node) return null;
let shouldDelete = deleteNums.has(node.val);
if(isRoot && !shouldDelete) output.push(node);
node.left = dfs(node.left, shouldDelete)
node.right = dfs(node.right, shouldDelete)
return shouldDelete? null:node;
}
dfs(head, true)
return output;
};
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
let arr = [1,2,3,null,null,null,4]
let to_delete = [2,1]
let node = new TreeNode(arr[0]);
let queue = [node]
let idx = 1;
while(queue.length){
let tempQueue = []
let node = queue.shift();
if(arr[idx] && arr[idx] !== null){
node.left = new TreeNode(arr[idx])
tempQueue.push(node.left)
}
if(arr[idx+1] && arr[idx+1] !== null){
node.right = new TreeNode(arr[idx+1])
tempQueue.push(node.right)
}
idx += 2
queue.push(...tempQueue)
}
delNodes(node, to_delete)
// @lc code=end
// let roots = [];
// let search = function(root, isRoot){
// if(!root) return null;
// let shouldDelete = to_delete.includes(root.val);
// if(isRoot && !shouldDelete) roots.push(root);
// root.left = search(root.left, shouldDelete);
// root.right = search(root.right, shouldDelete);
// return shouldDelete ? null:root;
// }
// search(root, true);
// return roots;
/*
// 1. BFS method to traverse (preorder)
// 2. Check if the value is included in to_delete array. shoudDelete to hold boolean value
// 3. When invoking recursive function, pass shouldDelete variable to next children tree
// 3-1. If shouldDelete is true, then children tree will be severed from the main tree and will be the starting root
// 4. If it is a Root and shouldDelete is false, then push it to output array
// 5. If shouldDelete is true, return null, else return root
let output = [];
let search = function(root, isRoot){
if(!root) return null;
let shouldDelete = to_delete.includes(root.val);
if(isRoot && !shouldDelete){
output.push(root)
}
root.left = search(root.left, shouldDelete);
root.right = search(root.right, shouldDelete);
return shouldDelete ? null:root
}
search(root, true);
return output;
*/