Simple CPP solution by TGK
IntuitionThe given problem is about recovering a binary tree that was corrupted and finding elements in it efficiently.ApproachConstructor (FindElements(TreeNod
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The given problem is about recovering a binary tree that was corrupted and finding elements in it efficiently.

    The tree follows a specific rule where if a node has value x, then:
        Its left child has value 2*x + 1
        Its right child has value 2*x + 2
    The constructor reconstructs the tree using this rule and stores all valid values in an unordered set (elements).
    The find() function simply checks if a given target exists in the set.

# Approach
<!-- Describe your approach to solving the problem. -->
Constructor (FindElements(TreeNode* root))

    Initialize an empty unordered_set<int> elements to store recovered values.
    Start from the root with value 0.
    Use a queue (BFS traversal):
        Assign 0 to the root.
        For each node:
            Compute left child value as 2*x + 1 (if left exists).
            Compute right child value as 2*x + 2 (if right exists).
            Store these values in the set.
    This reconstructs the tree efficiently.

Find Function (find(int target))

    Simply checks if target exists in the unordered_set<int> elements.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    Constructor: O(n)
    Find: O(1)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    Constructor: O(n)
    Find: O(1)
# 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 {
public:
    unordered_set<int> elements;
    FindElements(TreeNode* root) {
        if(root != nullptr){
            queue<pair<int, TreeNode*>> q;
            q.push({0, root});
            elements.insert(0);
            while(!q.empty()){
                auto [x, node] = q.front();
                q.pop();
                if(node->left != nullptr){
                    q.push({2*x + 1, node->left});
                    elements.insert(2*x + 1);
                }
                if(node -> right != nullptr){
                    q.push({2*x + 2, node -> right});
                    elements.insert(2*x + 2);
                }
            }
        }
    }
    
    bool find(int target) {
        return elements.find(target) != elements.end();
    }
};

/**
 * Your FindElements object will be instantiated and called as such:
 * FindElements* obj = new FindElements(root);
 * bool param_1 = obj->find(target);
 */
```
['C++']