Python | [EXPLAINED] | tree traversal
We can use any tree traversal method to make the tree contaminated, here we are using preorder traversal it is more favorable here since value of child nodes ar
* We can use any tree traversal method to make the tree contaminated, here we are using preorder traversal it is more favorable here since value of child nodes are dependent on the value of thier parent node.\n* For efficient target searching we will maintain a set and keep adding the values in it whenever we modify node value.\n\n```\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass FindElements:\n\n    def __init__(self, root: Optional[TreeNode]):\n        self.root = root\n        self.root.val = 0\n        self.st = set()\n        self.st.add(0)\n        \n        def preorder(root):\n            if root:\n                if root.left:\n                    root.left.val = 2*root.val+1\n                    self.st.add(2*root.val+1)\n                    preorder(root.left)\n                if root.right:\n                    root.right.val = 2*root.val+2\n                    self.st.add(2*root.val+2)\n                    preorder(root.right)\n        preorder(self.root)\n\n    def find(self, target: int) -> bool:\n        return True if target in self.st else False\n\n# Your FindElements object will be instantiated and called as such:\n# obj = FindElements(root)\n# param_1 = obj.find(target)\n```\n-----------------\n**Upvote the post if you find it helpful.**\n**Happy coding.**
['Python']