C++ || Array and Tree ✅ || Easiest Approach 🔥
\n# Approach\nForming a array to store binary tree. The values that is asked to store is similar to index that we use to store element of tree in array. Only st
\n# Approach\nForming a array to store binary tree. The values that is asked to store is similar to index that we use to store element of tree in array. Only storing a tree will take O(n). Otherwise find function will take O(1) time as it is only checking whether at target value, i.e., same as index of array is -1 or not.\n\n# Complexity\n- Time complexity: $$O(n)$$ ---- where n is the number of nodes in tree.\n\n# Code\n```\nclass FindElements {\n    vector<int> treeArr;\npublic:\n    FindElements(TreeNode* root) {\n        treeArr = vector<int>(1e6+1, -1);\n        dfs(root, 0);\n    }\n\n    void dfs(TreeNode* root, int i){\n        if(root == NULL || i > 1e6)\n            return;\n\n        treeArr[i] = i;\n        dfs(root->left, 2*i+1);\n        dfs(root->right, 2*i+2);\n    }\n    \n    bool find(int target) {\n        if(treeArr[target] == -1)\n            return false;\n\n        return true;\n    }\n};\n```\n\n---\n\n```\nIf you learn/found something new please upvote \uD83D\uDC4D\n```\n
['Tree', 'Depth-First Search', 'C++']