1261.cpp (658B)
1 class FindElements { 2 TreeNode *root; 3 unordered_set<int> all; 4 5 public: 6 FindElements(TreeNode *root) : root(root) { 7 queue<TreeNode *> q({root}); 8 root->val = 0; 9 while (!q.empty()) { 10 TreeNode *root = q.front(); 11 q.pop(); 12 all.insert(root->val); 13 if (root->left) { 14 root->left->val = 2 * root->val + 1; 15 q.push(root->left); 16 } 17 if (root->right) { 18 root->right->val = 2 * root->val + 2; 19 q.push(root->right); 20 } 21 } 22 } 23 24 bool find(int target) { return all.count(target); } 25 };