-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
53 lines (50 loc) · 1.24 KB
/
Program.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
using System;
using System.Collections.Generic;
namespace FindElementsInAContaminatedBinaryTree
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class FindElements
{
private HashSet<int> vals;
public FindElements(TreeNode root)
{
vals = new HashSet<int>();
root.val = 0;
vals.Add(0);
DFS(root, vals);
}
private void DFS(TreeNode node, HashSet<int> vals)
{
if (node == null) return;
if (node.left != null)
{
node.left.val = node.val * 2 + 1;
vals.Add(node.left.val);
}
if (node.right != null)
{
node.right.val = node.val * 2 + 2;
vals.Add(node.right.val);
}
DFS(node.left, vals);
DFS(node.right, vals);
}
public bool Find(int target)
{
return vals.Contains(target);
}
}
}