-
Notifications
You must be signed in to change notification settings - Fork 43
/
minimum-distance-between-bst-nodes.py
69 lines (64 loc) · 1.68 KB
/
minimum-distance-between-bst-nodes.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
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79402127
# IDEA : INORDER
# 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 minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
vals = []
def inOrder(root):
if not root:
return
inOrder(root.left)
vals.append(root.val)
inOrder(root.right)
inOrder(root)
return min([vals[i + 1] - vals[i] for i in range(len(vals) - 1)])
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79402127
class Solution:
def minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = float("inf")
self.prev = None
self.inOrder(root)
return self.res
def inOrder(self, root):
if not root: return
self.inOrder(root.left)
if self.prev:
self.res = min(self.res, root.val - self.prev.val)
self.prev = root
self.inOrder(root.right)
# V2
# Time: O(n)
# Space: O(h)
class Solution(object):
def minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def dfs(node):
if not node:
return
dfs(node.left)
self.result = min(self.result, node.val-self.prev)
self.prev = node.val
dfs(node.right)
self.prev = float('-inf')
self.result = float('inf')
dfs(root)
return self.result