-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy path270.py
More file actions
32 lines (26 loc) · 713 Bytes
/
270.py
File metadata and controls
32 lines (26 loc) · 713 Bytes
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Yu Zhou
# 270. Closest Binary Search Tree Value
# ****************
# Descrption:
# ****************
class Solution(object):
def closestValue(self, root, target):
#Edge Case:
if not root:
return 0
self.res = root.val
#Process
def dfs(root, target):
if not root:
return 0
if abs(target - self.res) > abs(target - root.val):
self.res = root.val
if target > root.val:
dfs(root.right, target)
else:
dfs(root.left, target)
# Recursion
dfs(root, target)
return self.res