Find Elements in a Contaminated Binary Tree LeetCode solution in C++
1class FindElements {2public:3unordered_map<int, int> m;45FindElements(TreeNode *root) {6if (root) {7root->val = 0;8queue<TreeNode *> q;9q.push(root);10m[0]++;1112while (!q.empty()) {13TreeNode *temp = q.front();14q.pop();15int x = temp->val;16if (temp->left) {17temp->left->val = x * 2 + 1;1819m[x * 2 + 1]++;20q.push(temp->left);21}2223if (temp->right) {24temp->right->val = x * 2 + 2;2526m[x * 2 + 2]++;27q.push(temp->right);28}29}30}31}3233bool find(int target) {34return (m.find(target) != m.end());35}36};