Advertisement
Guest User

Find Elements in a Contaminated Binary Tree

a guest
Apr 8th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10. class FindElements {
  11.     TreeNode* head;
  12.     int arr[1000001];
  13.     void recoverTree(TreeNode *root, int val){
  14.         if(!root)
  15.             return;
  16.         if(val > 1000000)
  17.             return;
  18.         arr[val] = 1;
  19.         root->val = val;
  20.         recoverTree(root->left, 2 * val + 1);
  21.         recoverTree(root->right, 2 * val + 2);
  22.     }
  23. public:
  24.     FindElements(TreeNode* root) {
  25.         head = root;
  26.         for(int i = 0; i < 1000001; i++)
  27.             arr[i] = 0;
  28.         recoverTree(root, 0);
  29.     }
  30.    
  31.     bool find(int target) {
  32.         return arr[target] == 1;
  33.     }
  34. };
  35.  
  36. /**
  37.  * Your FindElements object will be instantiated and called as such:
  38.  * FindElements* obj = new FindElements(root);
  39.  * bool param_1 = obj->find(target);
  40.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement