-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1110.cpp
37 lines (34 loc) · 995 Bytes
/
1110.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete)
{
vector<TreeNode*> res;
deleteNode(root, to_delete, res, true);
return res;
}
private:
TreeNode* deleteNode(TreeNode* root, vector<int>& to_delete, vector<TreeNode*>& res, bool no_parent)
{
if (!root) {
return root;
}
auto it = find(to_delete.begin(), to_delete.end(), root->val);
bool td = it != to_delete.end();
if (!td && no_parent) {
res.push_back(root);
}
root->left = deleteNode(root->left, to_delete, res, td);
root->right = deleteNode(root->right, to_delete, res, td);
return td? nullptr : root;
}
};