783. Minimum Distance Between BST Nodes
https://leetcode.com/problems/minimum-distance-between-bst-nodes
Python
In-Order Travel
from math import inf
from typing import Optional
class Solution:
def minDiffInBST(self, root: Optional[TreeNode]) -> int:
result = inf
pre = None
def inorder(node: Optional[TreeNode]):
if not node:
return
nonlocal pre
nonlocal result
inorder(node.left)
if pre is not None:
result = min(result, node.val-pre)
pre = node.val
inorder(node.right)
inorder(root)
return result