반응형
leetcode.com/problems/minimum-distance-between-bst-nodes/submissions/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
diff = sys.maxsize
prev = sys.maxsize
def minDiffInBST(self, root: TreeNode) -> int:
def recur(node):
if not node:
return
recur(node.right)
self.diff = min(self.diff, self.prev - node.val)
self.prev = node.val
recur(node.left)
return node
recur(root)
return self.diff
댓글