# 1261. Find Elements in a Contaminated Binary Tree



```
 T: O(n)
 S: O(n)
```

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class FindElements {

    private Set<Integer> set;
    public FindElements(TreeNode root) {
        set = new HashSet<>();
        set.add(0);
        dfs(root, 0);
    }
    
    public boolean find(int target) {
        return set.contains(target);
    }

    private void dfs(TreeNode node, int curValue) {
        if (node == null) {
            return;
        }
        set.add(curValue);

        dfs(node.left, curValue*2 + 1);
        dfs(node.right, curValue*2 + 2);
    }
}

/**
 * Your FindElements object will be instantiated and called as such:
 * FindElements obj = new FindElements(root);
 * boolean param_1 = obj.find(target);


 T: O(n)
 S: O(n)
 */
```
