Leetcode 1387 Solution

This article provides solution to leetcode question 1387 (find-elements-in-a-contaminated-binary-tree)

https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree

Solution

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class FindElements:

    def __init__(self, root: TreeNode):
        self.m = set()

        def recover(node, val):
            self.m.add(val)

            node.val = val

            if node.left:
                recover(node.left, 2 * val + 1)

            if node.right:
                recover(node.right, 2 * val + 2)

        if root:
            recover(root, 0)

    def find(self, target: int) -> bool:
        return target in self.m


# Your FindElements object will be instantiated and called as such:
# obj = FindElements(root)
# param_1 = obj.find(target)