-
Notifications
You must be signed in to change notification settings - Fork 1
/
270.closest-binary-search-tree-value.py
63 lines (60 loc) · 1.34 KB
/
270.closest-binary-search-tree-value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# @lc app=leetcode id=270 lang=python3
#
# [270] Closest Binary Search Tree Value
#
# https://leetcode.com/problems/closest-binary-search-tree-value/description/
#
# algorithms
# Easy (54.74%)
# Likes: 1647
# Dislikes: 94
# Total Accepted: 283.2K
# Total Submissions: 517.6K
# Testcase Example: '[4,2,5,1,3]\n3.714286'
#
# Given the root of a binary search tree and a target value, return the value
# in the BST that is closest to the target. If there are multiple answers,
# print the smallest.
#
#
# Example 1:
#
#
# Input: root = [4,2,5,1,3], target = 3.714286
# Output: 4
#
#
# Example 2:
#
#
# Input: root = [1], target = 4.428571
# Output: 1
#
#
#
# Constraints:
#
#
# The number of nodes in the tree is in the range [1, 10^4].
# 0 <= Node.val <= 10^9
# -10^9 <= target <= 10^9
#
#
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def closestValue(self, root: Optional[TreeNode], target: float) -> int:
closest = root.val
while root:
if abs(root.val - target) <= abs(closest - target):
closest = root.val
root = root.left if target < root.val else root.right
return closest
# @lc code=end