-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path1261-FindElementsInAContaminatedBinaryTree.cs
55 lines (48 loc) · 1.51 KB
/
1261-FindElementsInAContaminatedBinaryTree.cs
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
//-----------------------------------------------------------------------------
// Runtime: 156ms
// Memory Usage: 41.7 MB
// Link: https://leetcode.com/submissions/detail/360781980/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class _1261_FindElementsInAContaminatedBinaryTree
{
private readonly HashSet<int> values;
public _1261_FindElementsInAContaminatedBinaryTree(TreeNode root)
{
values = new HashSet<int>();
Recover(root, 0);
}
public bool Find(int target)
{
return values.Contains(target);
}
private void Recover(TreeNode node, int value)
{
if (node == null) return;
node.val = value;
values.Add(value);
Recover(node.left, 2 * value + 1);
Recover(node.right, 2 * value + 2);
}
}
/**
* Your FindElements object will be instantiated and called as such:
* FindElements obj = new FindElements(root);
* bool param_1 = obj.Find(target);
*/
}