Advertisement
serega1112

270

Mar 27th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. class Solution:
  2.     def closestValue(self, root: TreeNode, target: float) -> int:
  3.        
  4.         best = float('inf')
  5.        
  6.         while root:
  7.             if abs(target - root.val) < abs(target - best):
  8.                 best = root.val
  9.             if root.val > target:
  10.                 root = root.left
  11.             elif root.val < target:
  12.                 root = root.right
  13.             else:
  14.                 break
  15.                
  16.         return best
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement