Closest Binary Search Tree Value

二分法往下搜索就行了,主要是要追求代码简洁

class Solution(object):
    def closestValue(self, root, target):
        """
        :type root: TreeNode
        :type target: float
        :rtype: int
        """
        result = sys.maxint
        while root:
            if abs(target - root.val) < abs(result - target):
                result = root.val
            root = root.left if root.val > target else root.right

        return result

results matching ""

    No results matching ""