forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1261.cpp
More file actions
34 lines (31 loc) · 646 Bytes
/
1261.cpp
File metadata and controls
34 lines (31 loc) · 646 Bytes
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
class FindElements
{
public:
FindElements(TreeNode* root)
{
root->val = 0;
data.insert(0);
dfs(root);
}
bool find(int target)
{
return data.count(target);
}
private:
unordered_set<int> data;
void dfs(TreeNode* root)
{
if (root->left)
{
root->left->val = 2 * root->val + 1;
data.insert(root->left->val);
dfs(root->left);
}
if (root->right)
{
root->right->val = 2 * root->val + 2;
data.insert(root->right->val);
dfs(root->right);
}
}
};