-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1261.cpp
More file actions
executable file
·34 lines (30 loc) · 772 Bytes
/
LC1261.cpp
File metadata and controls
executable file
·34 lines (30 loc) · 772 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
/*
Problem Statement: https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
|--------------------|------|-------|
| Operations | Time | Space |
|--------------------|------|-------|
| FindElements(root) | O(n) | O(n) |
| fix(val, node) | O(n) | O(n) |
| find(target) | O(1) | O(1) |
|--------------------|------|-------|
*/
class FindElements {
private:
unordered_set<int> seen;
public:
FindElements(TreeNode* root) {
fix(0, root);
}
void fix(int val, TreeNode* node) {
if (!node)
return;
seen.insert(val);
fix(2 * val + 1, node->left);
fix(2 * val + 2, node->right);
}
bool find(int target) {
return seen.find(target) != seen.end();
}
};