270 Closest Binary Search Tree Value
class Solution:
def closestValue(self, root, target):
"""
:type root: TreeNode
:type target: float
:rtype: int
"""
min_diff = [sys.maxsize]
min_val = [root.val]
def search(root):
if not root:
return
cur_min_diff = abs(root.val - target)
if cur_min_diff < min_diff[0]:
min_diff[0] = cur_min_diff
min_val[0] = root.val
if target < root.val:
return search(root.left)
else:
return search(root.right)
search(root)
return min_val[0]Last updated