给定一棵二叉搜索树,请你返回树中任意两节点之差的最小值。
数据范围:二叉树节点数满足
,二叉树的节点值满足
,保证每个节点的值都不同
class Solution:
def minDifference(self , root: TreeNode) -> int:
# write code here
l = []
def inOrder(root):
if not root:
return
inOrder(root.left)
l.append(root.val)
inOrder(root.right)
inOrder(root)
ans = float('inf')
for i in range(1, len(l)):
ans = min(ans, l[i] - l[i-1])
return ans