• root.val == 0

  • If treeNode.val is x and treeNode.left is not null, then treeNode.left.val = 2 * x + 1

  • If treeNode.v">

    Find Elements in a Contaminated Binary Tree in C++



    Suppose we have a binary tree. The rules of that tree is as follows −

    • root.val == 0

    • If treeNode.val is x and treeNode.left is not null, then treeNode.left.val = 2 * x + 1

    • If treeNode.val is x and treeNode.right is not null, then treeNode.right.val = 2 * x + 2

    Now as the binary tree is contaminated. This indicates all val of the tree node have been changed to -1. We have to first recover the binary tree and then implement the FindElements class as follows −

    • FindElements(TreeNode* root) Initializes the object with a contamined binary tree, we have to recover it first.

    • bool find(int target). This will return if the target value exists in the recovered binary tree.

    So if the tree is like −


    So after recovering, if we try to find 1, 3 and 5, then the results will be true, true and false.

    To solve this, we will follow these steps −

    • Define a set a

    • Define a dfs() method, this will take root, and rootVal. rootVal is initially -1

    • if root is null, then return

    • if rootVal is -1, then set value of root as 0, otherwise set it as rootVal

    • insert value of root into a

    • call dfs(left of root, 2* value of root + 1), dfs(right of root, 2* value of root + 2)

    • For the initialization, (or reconstruction), we will call dfs(root, -1)

    • To find some element, check whether the element will be there in a or not, if so return true, otherwise false.

    Let us see the following implementation to get better understanding −

    Example

     Live Demo

    #include <bits/stdc++.h>
    using namespace std;
    class TreeNode{
       public:
       int val;
       TreeNode *left, *right;
       TreeNode(int data){
          val = data;
          left = NULL;
          right = NULL;
       }
    };
    void insert(TreeNode **root, int val){
       queue<TreeNode*> q;
       q.push(*root);
       while(q.size()){
          TreeNode *temp = q.front();
          q.pop();
          if(!temp->left){
             if(val != NULL)
                temp->left = new TreeNode(val);
             else
                temp->left = new TreeNode(0);
             return;
          }else{
             q.push(temp->left);
          }
          if(!temp->right){
             if(val != NULL)
                temp->right = new TreeNode(val);
             else
                temp->right = new TreeNode(0);
             return;
          }else{
             q.push(temp->right);
          }
       }
    }
    TreeNode *make_tree(vector<int> v){
       TreeNode *root = new TreeNode(v[0]);
       for(int i = 1; i<v.size(); i++){
          insert(&root, v[i]);
       }
       return root;
    }
    class FindElements {
       public:
       set <int> a;
       void dfs(TreeNode* root,int rootVal=-1){
          if(!root)return;
          root->val = rootVal == -1?0:rootVal;
          a.insert(root->val);
          dfs(root->left,2*root->val + 1);
          dfs(root->right, 2*root->val + 2);
       }
       FindElements(TreeNode* root) {
          dfs(root);
       }
       bool find(int t) {
          return a.find(t)!=a.end();
       }
    };
    main(){
       vector<int> v = {-1,-1,-1,-1,-1};
       TreeNode *root = make_tree(v);
       FindElements ob(root);
       cout << (ob.find(1)) << endl;
       cout << (ob.find(3)) << endl;
       cout << (ob.find(5)) << endl;
    }

    Input

    Initialize the tree with [-1,-1,-1,-1,-1], then call find(1), find(3) and find(5)

    Output

    1
    1
    0
    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements