-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1261-elements-contaminated-tree.dart
66 lines (55 loc) · 1.56 KB
/
1261-elements-contaminated-tree.dart
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Definition for a binary tree node.
* class TreeNode {
* int val;
* TreeNode? left;
* TreeNode? right;
* TreeNode([this.val = 0, this.left, this.right]);
* }
*/
class TreeNode {
int val;
TreeNode? left;
TreeNode? right;
TreeNode([this.val = 0, this.left, this.right]);
}
class FindElements {
Set<int> _values = {};
FindElements(TreeNode? root) {
_recoverTree(root, 0);
}
void _recoverTree(TreeNode? node, int val) {
if (node == null) return;
node.val = val;
_values.add(val);
_recoverTree(node.left, 2 * val + 1);
_recoverTree(node.right, 2 * val + 2);
}
bool find(int target) {
return _values.contains(target);
}
}
/**
* Your FindElements object will be instantiated and called as such:
* FindElements obj = FindElements(root);
* bool param1 = obj.find(target);
*/
void main() {
// Test Case 1
TreeNode root1 = TreeNode(-1, TreeNode(-1), TreeNode(-1));
FindElements findElements1 = FindElements(root1);
print(findElements1.find(1)); // true
print(findElements1.find(2)); // true
print(findElements1.find(3)); // false
// Test Case 2
TreeNode root2 = TreeNode(-1, null, TreeNode(-1));
FindElements findElements2 = FindElements(root2);
print(findElements2.find(1)); // false
print(findElements2.find(2)); // true
// Test Case 3
TreeNode root3 = TreeNode(-1, TreeNode(-1, TreeNode(-1), null), TreeNode(-1));
FindElements findElements3 = FindElements(root3);
print(findElements3.find(1)); // true
print(findElements3.find(3)); // true
print(findElements3.find(5)); // false
}