given a nonempty binary search tree and a target value find the value in the bst that is closest to the target note given target value is a floating point you are guaranteed to have only one unique value in the bst that is closest to the target definition for a binary tree node class treenodeobject def initself x self val x self left none self right none type root treenode type target float rtype int given a non empty binary search tree and a target value find the value in the bst that is closest to the target note given target value is a floating point you are guaranteed to have only one unique value in the bst that is closest to the target definition for a binary tree node class treenode object def __init__ self x self val x self left none self right none type root treenode type target float rtype int
def closest_value(root, target):
    
    a = root.val
    kid = root.left if target < a else root.right
    if not kid:
        return a
    b = closest_value(kid, target)
    return min((a,b), key=lambda x: abs(target-x))