-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeleteNodesAndReturnForest_1110.cpp
51 lines (40 loc) · 1.39 KB
/
DeleteNodesAndReturnForest_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 1110. Delete Nodes And Return Forest
~ Link : https://leetcode.com/problems/delete-nodes-and-return-forest/
*/
/**
* 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:
unordered_set<int> toDelete;
TreeNode* delNodesUtil(TreeNode *root, vector<TreeNode*> &res, bool isRoot) {
if (!root)
return NULL;
bool deleteCurrNode = (toDelete.find(root -> val) != toDelete.end());
if (isRoot && !deleteCurrNode)
res.emplace_back(root);
root -> left = delNodesUtil(root -> left, res, deleteCurrNode);
root -> right = delNodesUtil(root -> right, res, deleteCurrNode);
return (deleteCurrNode ? NULL : root);
}
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
vector<TreeNode*> res;
for (int key : to_delete) {
toDelete.insert(key);
}
delNodesUtil(root, res, true);
return res;
}
};
// T.C. - O(n)
// S.C. - O(h + n)