45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
- ** 方法一:后序遍历**
48
+ ** 方法一:DFS**
49
+
50
+ 我们先用哈希表或者一个长度为 $1001$ 的数组 $s$ 记录所有需要删除的节点。
51
+
52
+ 接下来,设计一个函数 $dfs(root)$,它会返回以 $root$ 为根的子树中,删除所有需要删除的节点后的树的根节点。函数 $dfs(root)$ 的执行逻辑如下:
53
+
54
+ - 如果 $root$ 为空,那么我们返回空;
55
+ - 否则,我们递归执行 $dfs(root.left)$ 和 $dfs(root.right)$,并将返回值分别赋给 $root.left$ 和 $root.right$。如果 $root$ 不需要被删除,那么我们返回 $root$;如果 $root$ 需要被删除,那么我们分别判断 $root.left$ 和 $root.right$ 是否为空,如果它们不为空,那么我们将它们加入答案数组中;最后返回空。
56
+
57
+ 在主函数中,我们调用 $dfs(root)$,如果结果不为空,说明根节点不需要被删除,我们再将根节点加入答案数组中。最后返回答案数组即可。
49
58
50
59
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树的节点数。
51
60
63
72
# self.left = left
64
73
# self.right = right
65
74
class Solution :
66
- def delNodes (self , root : TreeNode, to_delete : List[int ]) -> List[TreeNode]:
67
- def dfs (fa , root ):
75
+ def delNodes (
76
+ self , root : Optional[TreeNode], to_delete : List[int ]
77
+ ) -> List[TreeNode]:
78
+ def dfs (root : Optional[TreeNode]) -> Optional[TreeNode]:
68
79
if root is None :
69
- return
70
- dfs(root, root.left)
71
- dfs(root, root.right)
72
- if root.val in s:
73
- if fa and fa.left == root:
74
- fa.left = None
75
- if fa and fa.right == root:
76
- fa.right = None
77
- if root.left:
78
- ans.append(root.left)
79
- if root.right:
80
- ans.append(root.right)
80
+ return None
81
+ root.left, root.right = dfs(root.left), dfs(root.right)
82
+ if root.val not in s:
83
+ return root
84
+ if root.left:
85
+ ans.append(root.left)
86
+ if root.right:
87
+ ans.append(root.right)
88
+ return None
81
89
82
90
s = set (to_delete)
83
91
ans = []
84
- if root.val not in s :
92
+ if dfs( root) :
85
93
ans.append(root)
86
- dfs(None , root)
87
94
return ans
88
95
```
89
96
@@ -108,82 +115,35 @@ class Solution:
108
115
* }
109
116
*/
110
117
class Solution {
111
- public List<TreeNode > delNodes (TreeNode root , int [] to_delete ) {
112
- boolean [] del = new boolean [1001 ];
113
- for (int d : to_delete) {
114
- del[d] = true ;
115
- }
116
- List<TreeNode > res = new ArrayList<> ();
117
- dfs(root, true , del, res);
118
- return res;
119
- }
120
-
121
- private TreeNode dfs (TreeNode root , boolean isRoot , boolean [] del , List<TreeNode > res ) {
122
- if (root == null ) {
123
- return null ;
124
- }
125
- boolean flag = del[root. val];
126
- if (! flag && isRoot) {
127
- res. add(root);
128
- }
129
- root. left = dfs(root. left, flag, del, res);
130
- root. right = dfs(root. right, flag, del, res);
131
- return flag ? null : root;
132
- }
133
- }
134
- ```
135
-
136
- ``` java
137
- /**
138
- * Definition for a binary tree node.
139
- * public class TreeNode {
140
- * int val;
141
- * TreeNode left;
142
- * TreeNode right;
143
- * TreeNode() {}
144
- * TreeNode(int val) { this.val = val; }
145
- * TreeNode(int val, TreeNode left, TreeNode right) {
146
- * this.val = val;
147
- * this.left = left;
148
- * this.right = right;
149
- * }
150
- * }
151
- */
152
- class Solution {
118
+ private boolean [] s = new boolean [1001 ];
153
119
private List<TreeNode > ans = new ArrayList<> ();
154
- private Set<Integer > s = new HashSet<> ();
155
120
156
121
public List<TreeNode > delNodes (TreeNode root , int [] to_delete ) {
157
- for (int v : to_delete) {
158
- s. add(v) ;
122
+ for (int x : to_delete) {
123
+ s[x] = true ;
159
124
}
160
- if (! s . contains (root. val) ) {
125
+ if (dfs (root) != null ) {
161
126
ans. add(root);
162
127
}
163
- dfs(null , root);
164
128
return ans;
165
129
}
166
130
167
- private void dfs (TreeNode fa , TreeNode root ) {
131
+ private TreeNode dfs (TreeNode root ) {
168
132
if (root == null ) {
169
- return ;
133
+ return null ;
170
134
}
171
- dfs(root, root. left);
172
- dfs(root, root. right);
173
- if (s. contains(root. val)) {
174
- if (fa != null && fa. left == root) {
175
- fa. left = null ;
176
- }
177
- if (fa != null && fa. right == root) {
178
- fa. right = null ;
179
- }
180
- if (root. left != null ) {
181
- ans. add(root. left);
182
- }
183
- if (root. right != null ) {
184
- ans. add(root. right);
185
- }
135
+ root. left = dfs(root. left);
136
+ root. right = dfs(root. right);
137
+ if (! s[root. val]) {
138
+ return root;
139
+ }
140
+ if (root. left != null ) {
141
+ ans. add(root. left);
186
142
}
143
+ if (root. right != null ) {
144
+ ans. add(root. right);
145
+ }
146
+ return null ;
187
147
}
188
148
}
189
149
```
@@ -205,23 +165,33 @@ class Solution {
205
165
class Solution {
206
166
public:
207
167
vector<TreeNode* > delNodes(TreeNode* root, vector<int >& to_delete) {
168
+ bool s[ 1001] ;
169
+ memset(s, 0, sizeof(s));
170
+ for (int x : to_delete) {
171
+ s[ x] = true;
172
+ }
208
173
vector<TreeNode* > ans;
209
- unordered_set<int > s(to_delete.begin(), to_delete.end());
210
- if (!s.count(root->val)) ans.push_back(root);
211
- dfs(nullptr, root, s, ans);
212
- return ans;
213
- }
214
-
215
- void dfs(TreeNode* fa, TreeNode* root, unordered_set<int>& s, vector<TreeNode*>& ans) {
216
- if (!root) return;
217
- dfs(root, root->left, s, ans);
218
- dfs(root, root->right, s, ans);
219
- if (s.count(root->val)) {
220
- if (fa && fa->left == root) fa->left = nullptr;
221
- if (fa && fa->right == root) fa->right = nullptr;
222
- if (root->left) ans.push_back(root->left);
223
- if (root->right) ans.push_back(root->right);
174
+ function<TreeNode* (TreeNode* )> dfs = [ &] (TreeNode* root) -> TreeNode* {
175
+ if (!root) {
176
+ return nullptr;
177
+ }
178
+ root->left = dfs(root->left);
179
+ root->right = dfs(root->right);
180
+ if (!s[ root->val] ) {
181
+ return root;
182
+ }
183
+ if (root->left) {
184
+ ans.push_back(root->left);
185
+ }
186
+ if (root->right) {
187
+ ans.push_back(root->right);
188
+ }
189
+ return nullptr;
190
+ };
191
+ if (dfs(root)) {
192
+ ans.push_back(root);
224
193
}
194
+ return ans;
225
195
}
226
196
};
227
197
```
@@ -237,40 +207,83 @@ public:
237
207
* Right *TreeNode
238
208
* }
239
209
*/
240
- func delNodes (root *TreeNode , to_delete []int ) []*TreeNode {
241
- s := map [ int ]bool {}
242
- for _ , v := range to_delete {
243
- s[v ] = true
210
+ func delNodes(root *TreeNode, to_delete []int) (ans []*TreeNode) {
211
+ s := make([ ]bool, 1001)
212
+ for _, x := range to_delete {
213
+ s[x ] = true
244
214
}
245
- ans := []*TreeNode{}
246
- if !s[root.Val ] {
247
- ans = append (ans, root)
248
- }
249
- var fa *TreeNode
250
- var dfs func (fa, root *TreeNode)
251
- dfs = func (fa, root *TreeNode) {
215
+ var dfs func(*TreeNode) *TreeNode
216
+ dfs = func(root *TreeNode) *TreeNode {
252
217
if root == nil {
253
- return
218
+ return nil
219
+ }
220
+ root.Left = dfs(root.Left)
221
+ root.Right = dfs(root.Right)
222
+ if !s[root.Val] {
223
+ return root
254
224
}
255
- dfs (root, root.Left )
256
- dfs (root, root.Right )
257
- if s[root.Val ] {
258
- if fa != nil && fa.Left == root {
259
- fa.Left = nil
260
- }
261
- if fa != nil && fa.Right == root {
262
- fa.Right = nil
263
- }
264
- if root.Left != nil {
265
- ans = append (ans, root.Left )
266
- }
267
- if root.Right != nil {
268
- ans = append (ans, root.Right )
269
- }
225
+ if root.Left != nil {
226
+ ans = append(ans, root.Left)
270
227
}
228
+ if root.Right != nil {
229
+ ans = append(ans, root.Right)
230
+ }
231
+ return nil
232
+ }
233
+ if dfs(root) != nil {
234
+ ans = append(ans, root)
271
235
}
272
- dfs (fa, root)
273
- return ans
236
+ return
237
+ }
238
+ ```
239
+
240
+ ### ** TypeScript**
241
+
242
+ ``` ts
243
+ /**
244
+ * Definition for a binary tree node.
245
+ * class TreeNode {
246
+ * val: number
247
+ * left: TreeNode | null
248
+ * right: TreeNode | null
249
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
250
+ * this.val = (val===undefined ? 0 : val)
251
+ * this.left = (left===undefined ? null : left)
252
+ * this.right = (right===undefined ? null : right)
253
+ * }
254
+ * }
255
+ */
256
+
257
+ function delNodes(
258
+ root : TreeNode | null ,
259
+ to_delete : number [],
260
+ ): Array <TreeNode | null > {
261
+ const s: boolean [] = Array (1001 ).fill (false );
262
+ for (const x of to_delete ) {
263
+ s [x ] = true ;
264
+ }
265
+ const ans: Array <TreeNode | null > = [];
266
+ const dfs = (root : TreeNode | null ): TreeNode | null => {
267
+ if (! root ) {
268
+ return null ;
269
+ }
270
+ root .left = dfs (root .left );
271
+ root .right = dfs (root .right );
272
+ if (! s [root .val ]) {
273
+ return root ;
274
+ }
275
+ if (root .left ) {
276
+ ans .push (root .left );
277
+ }
278
+ if (root .right ) {
279
+ ans .push (root .right );
280
+ }
281
+ return null ;
282
+ };
283
+ if (dfs (root )) {
284
+ ans .push (root );
285
+ }
286
+ return ans ;
274
287
}
275
288
```
276
289
0 commit comments