-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1261.cpp
More file actions
35 lines (33 loc) · 969 Bytes
/
1261.cpp
File metadata and controls
35 lines (33 loc) · 969 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
35
/**
* 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:
unordered_set<int> st;
void dfs(TreeNode* node, int current) {
if (!node) return;
node->val = current;
st.insert(current);
if (node->left) dfs(node->left, current * 2 + 1);
if (node->right) dfs(node->right, current * 2 + 2);
}
FindElements(TreeNode* root) {
dfs(root, 0);
}
bool find(int target) {
return st.count(target) > 0;
}
};
/**
* Your FindElements object will be instantiated and called as such:
* FindElements* obj = new FindElements(root);
* bool param_1 = obj->find(target);
*/