-
Notifications
You must be signed in to change notification settings - Fork 43
/
minimum-absolute-difference-in-bst.py
71 lines (64 loc) · 2.16 KB
/
minimum-absolute-difference-in-bst.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
63
64
65
66
67
68
69
70
71
# V0
# V1
# http://bookshadow.com/weblog/2017/02/26/leetcode-minimum-absolute-difference-in-bst/
# 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 getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.last = -0x80000000
self.ans = 0x7FFFFFFF
def inOrderTraverse(root):
if not root: return
inOrderTraverse(root.left)
self.ans = min(self.ans, root.val - self.last) # since there is no "root.val - self.last" value when 1st func call, so we use self.ans to deal with this
self.last = root.val
inOrderTraverse(root.right)
inOrderTraverse(root)
return self.ans
# V1'
# http://bookshadow.com/weblog/2017/02/26/leetcode-minimum-absolute-difference-in-bst/
# 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 getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
left, right = root.left, root.right
ans = 0x7FFFFFFF
if left:
while left.right: left = left.right
ans = min(root.val - left.val, self.getMinimumDifference(root.left))
if right:
while right.left: right = right.left
ans = min(ans, right.val - root.val, self.getMinimumDifference(root.right))
return ans
# V2
# Time: O(n)
# Space: O(h)
class Solution(object):
def getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def inorderTraversal(root, prev, result):
if not root:
return (result, prev)
result, prev = inorderTraversal(root.left, prev, result)
if prev: result = min(result, root.val - prev.val)
return inorderTraversal(root.right, root, result)
return inorderTraversal(root, None, float("inf"))[0]