-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
54 lines (44 loc) · 1.05 KB
/
test.cpp
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
#include <cstdarg>
#include <type_traits>
#include <unordered_set>
#include "iostream"
#include "map"
#include "math.h"
#include "vector"
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<TreeNode *> res;
unordered_set<int> del;
void dfs(TreeNode *&root) {
if (root == NULL) return;
dfs(root->left);
dfs(root->right);
if (del.find(root->val) != del.end()) {
if (root->left) res.push_back(root->left);
if (root->right) res.push_back(root->right);
root = NULL;
delete root;
}
}
vector<TreeNode *> delNodes(TreeNode *root, vector<int> &to_delete) {
for (int i = 0; i < to_delete.size(); i++) {
del.insert(to_delete[i]);
}
dfs(root);
res.push_back(root);
return res;
}
};
int main() {
Solution s;
return 0;
}