class Solution:
    def find_value(self, root, target):
        if not root: return
        if abs(self.res - target) > abs(root.val - target):
            self.res = root.val
        if root.val == target: return
        if root.val < target:
            self.find_value(root.right, target)
        else:
            self.find_value(root.left, target)
        return

    def closestValue(self, root, target):
        self.res = float("inf")
        self.find_value(root, target)
        return self.res
        