Simple solution | DFS
IntuitionWe will iterate over the Tree using a DFS and update the values if we ecounter a -1 and store them in a map to check for occurances.Approach
Use DFS to
# Intuition
We will iterate over the Tree using a DFS and update the values if we ecounter a -1 and store them in a map to check for occurances.
# Approach
1. Use DFS to iterate over the tree.
2. If a node has -1 as the value, update it to the expected value.
3. If the children are present, iterate over them with their respective expected values.
4. Mark the values in a map as we traverse the tree.
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(n)

# Code
```cpp []
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class FindElements {

private:

    unordered_map<int,int> mpp;

    void solve(TreeNode* node, int exp){
        if(!node){return;}
        if(node->val==-1){node->val=exp;}
        mpp[node->val]++;
        if(node->left){
            solve(node->left,2*exp+1);
        }
        if(node->right){
            solve(node->right,2*exp+2);
        }
    }

public:
    FindElements(TreeNode* root) {
        mpp.clear();
        solve(root,0);
    }
    
    bool find(int target) {
        return mpp.find(target)!=mpp.end();
    }
};
```

```java []
 private HashSet<Integer> values;

    private void solve(TreeNode node, int val) {
        if (node == null) return;
        node.val = val;
        values.add(val);
        solve(node.left, 2 * val + 1);
        solve(node.right, 2 * val + 2);
    }

    public FindElements(TreeNode root) {
        values = new HashSet<>();
        solve(root, 0);
    }

    public boolean find(int target) {
        return values.contains(target);
    }
```

```python []
class FindElements:
    def __init__(self, root: TreeNode):
        self.values = set()
        self.solve(root, 0)

    def solve(self, node, val):
        if not node:
            return
        node.val = val
        self.values.add(val)
        self.solve(node.left, 2 * val + 1)
        self.solve(node.right, 2 * val + 2)

    def find(self, target: int) -> bool:
        return target in self.values
```

Please upvote if you found it useful!!
['Hash Table', 'Tree', 'Depth-First Search', 'Design', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3']