Hash Set and Depth-First Search
Complexity

Time complexity:
O(n)fornew FindElements(root)
O(1)forFindElements.find(target)


Space complexity:
O(n)

Code
# Complexity
- Time complexity:
$$O(n)$$ for `new FindElements(root)`
$$O(1)$$ for `FindElements.find(target)`

- Space complexity:
$$O(n)$$

# Code
```typescript []
class FindElements {
    private set = new Set<number>();
    constructor(root: TreeNode | null) {
        const dfs = (node: TreeNode | null, val: number) => {
            if (!node) return;
            this.set.add(val);
            dfs(node.left, val = val * 2 + 1);
            dfs(node.right, val + 1);
        };
        dfs(root, 0);
    }
    find = (target: number) => this.set.has(target);
}
```
```javascript []
class FindElements {
    constructor(root) {
        this.set = new Set();
        const dfs = (node, val) => {
            if (!node) return;
            this.set.add(val);
            dfs(node.left, val = val * 2 + 1);
            dfs(node.right, val + 1);
        };   
        dfs(root, 0);
    }
    find = (target) => this.set.has(target);
}
```
['Hash Table', 'Math', 'Tree', 'Depth-First Search', 'Design', 'Simulation', 'Binary Tree', 'TypeScript', 'JavaScript']