- Easy
- Given the
root
of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree. - https://leetcode.com/problems/minimum-distance-between-bst-nodes/
Runtime: 36 msMemory Usage: 14.2 MB
class Solution(object):
pre = -float('inf')
res = float('inf')
def minDiffInBST(self, root):
if root.left:
self.minDiffInBST(root.left)
self.res = min(self.res, root.val - self.pre)
self.pre = root.val
if root.right:
self.minDiffInBST(root.right)
return self.res
{% hint style="info" %} Inorder traversal. {% endhint %}