-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindElementsinaContaminatedBinaryTree.cpp
More file actions
41 lines (37 loc) · 1.04 KB
/
findElementsinaContaminatedBinaryTree.cpp
File metadata and controls
41 lines (37 loc) · 1.04 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
// Source: https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/
// Author: Miao Zhang
// Date: 2021-04-20
/**
* 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 FindElements {
public:
FindElements(TreeNode* root) {
recover(root, 0);
}
bool find(int target) {
return s_.count(target);
}
private:
unordered_set<int> s_;
void recover(TreeNode* root, int val) {
if (!root) return;
root->val = val;
s_.insert(val);
recover(root->left, 2 * val + 1);
recover(root->right, 2 * val + 2);
}
};
/**
* Your FindElements object will be instantiated and called as such:
* FindElements* obj = new FindElements(root);
* bool param_1 = obj->find(target);
*/