Python binary path without set
bin(9 + 1) = 1 010\nStart from the second digit: 0/1/0 means left/right/left when traverse if 9 exists.\nrecover: T: O(n), S: O(n) (DFS worst case)\nfind: T: O(
bin(9 + 1) = 1 010\nStart from the second digit: 0/1/0 means left/right/left when traverse if 9 exists.\nrecover: T: O(n), S: O(n) (DFS worst case)\nfind: T: O(logn), S: O(logn) (for the string of path encoding, can be O(1) if use bit mask) \n```\nclass FindElements(object):\n\n    def __init__(self, root):\n        """\n        :type root: TreeNode\n        """\n        self.root = root\n        if not root:\n            return\n        root.val = 0\n        self.recover(root)\n        return\n    def recover(self, root):\n        if not root:\n            return\n        if root.left:\n            root.left.val = root.val * 2 + 1\n            self.recover(root.left)\n        if root.right:\n            root.right.val = root.val * 2 + 2\n            self.recover(root.right)\n\n    def find(self, target):\n        """\n        :type target: int\n        :rtype: bool\n        """\n        node = self.root\n        encoding = bin(target + 1)[3:]\n        counter = 0\n        while counter < len(encoding):\n            if encoding[counter] == "0":\n                if node.left:\n                    node = node.left\n                    counter += 1\n                else:\n                    return False\n            else:\n                if node.right:\n                    node = node.right\n                    counter += 1\n                else:\n                    return False\n        return True
[]