-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1110.DeleteNodesAndReturnForest.cpp
More file actions
49 lines (41 loc) · 1.46 KB
/
1110.DeleteNodesAndReturnForest.cpp
File metadata and controls
49 lines (41 loc) · 1.46 KB
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
/**
* Definition for a binary tree node.
* 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:
set<int> remainingValues;
vector<TreeNode*> forest;
TreeNode* processForest(TreeNode* root, bool hasParent = false) {
if (root == nullptr) return nullptr;
// Check if node is to be removed.
auto it = remainingValues.find(root->val);
const bool remove = it != remainingValues.end();
if (remove) remainingValues.erase(it);
// Add if root and not removed.
if (!remove && !hasParent) forest.emplace_back(root);
// Process children.
root->left = processForest(root->left, !remove);
root->right = processForest(root->right, !remove);
// Return valid self / removed pointer.
return remove ? nullptr : root;
}
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Setup data.
remainingValues = set<int>(to_delete.begin(), to_delete.end());
// Get forest (group of trees).
root = processForest(root);
return forest;
}
};