Finding Elements in a Contaminated Binary Tree || DFS || JAVA ✅
IntuitionThe problem involves recovering a corrupted binary tree where each node's value was replaced with -1. We need to reconstruct the values such that:The r
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves recovering a corrupted binary tree where each node's value was replaced with -1. We need to reconstruct the values such that:

The root is 0.
The left child of a node with value x has value 2*x + 1.
The right child of a node with value x has value 2*x + 2.
Since we need to efficiently check if a value exists in the tree, we use a BitSet for fast lookups.

# Approach
<!-- Describe your approach to solving the problem. -->
**Tree Recovery using DFS (Inorder Traversal)**

- Traverse the tree starting from the root.
- Assign the correct values to each node based on the given formula.
- Store the values in a BitSet for fast O(1) lookups.
- Fast Lookups with BitSet

**BitSet allows checking if a value is present in constant time using get(target), making the find operation very efficient.**


# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    Recovering the tree : O(N)
    Checking the elements: O(1)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    Bitset: O(N)

# Code
```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 {
    TreeNode root;
    BitSet sett;

    public void inorder(TreeNode root, int val) {
        if (root == null) return;
        root.val = val;
        sett.set(val);
        inorder(root.left, 2 * val + 1);
        inorder(root.right, 2 * val + 2);
    }

    public FindElements(TreeNode root) {
        this.root = root;
        sett = new BitSet();
        inorder(this.root, 0);
    }

    public boolean find(int target) {
        return sett.get(target);
    }
}


/**
 * Your FindElements object will be instantiated and called as such:
 * FindElements obj = new FindElements(root);
 * boolean param_1 = obj.find(target);
 */
```
['Tree', 'Depth-First Search', 'Binary Tree', 'Java']