-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-1110.cpp
36 lines (30 loc) · 939 Bytes
/
Problem-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
//Problem - 1110
// https://leetcode.com/problems/delete-nodes-and-return-forest/
// Time Complexity O(n) and O(1) space ignoring stack space
class Solution {
public:
unordered_set <int> toDel;
vector <TreeNode*> ans;
TreeNode* deleteNode(TreeNode* root) {
if(!root)
return NULL;
root->left = deleteNode(root->left);
root->right = deleteNode(root->right);
if(toDel.find(root->val) != toDel.end()) {
if(root->left)
ans.push_back(root->left);
if(root->right)
ans.push_back(root->right);
return NULL;
}
return root;
}
vector <TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
for(auto i : to_delete)
toDel.insert(i);
TreeNode* temp = deleteNode(root);
if(temp)
ans.push_back(root);
return ans;
}
};