-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosest Binary Search Tree Value.py
51 lines (37 loc) · 1.07 KB
/
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
'''
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.
Example:
Input: root = [4,2,5,1,3], target = 3.714286
4
/ \
2 5
/ \
1 3
Output: 4
'''
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def closestValue(self, root, target):
"""
:type root: TreeNode
:type target: float
:rtype: int
"""
res = [root.val]
self.find(root, target, res)
return res[0]
def find(self, node, target, res):
if abs(res[0] - target) > abs(node.val - target):
res[0] = node.val
if node.left:
self.find(node.left, target, res)
if node.right:
self.find(node.right, target, res)