Find Elements in a Contaminated Binary Tree LeetCode solution in C++

1
class FindElements {
2
public:
3
unordered_map<int, int> m;
4

5
FindElements(TreeNode *root) {
6
if (root) {
7
root->val = 0;
8
queue<TreeNode *> q;
9
q.push(root);
10
m[0]++;
11

12
while (!q.empty()) {
13
TreeNode *temp = q.front();
14
q.pop();
15
int x = temp->val;
16
if (temp->left) {
17
temp->left->val = x * 2 + 1;
18

19
m[x * 2 + 1]++;
20
q.push(temp->left);
21
}
22

23
if (temp->right) {
24
temp->right->val = x * 2 + 2;
25

26
m[x * 2 + 2]++;
27
q.push(temp->right);
28
}
29
}
30
}
31
}
32

33
bool find(int target) {
34
return (m.find(target) != m.end());
35
}
36
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0