题目:
给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
差值是一个正数,其数值等于两值之差的绝对值。
示例 1:
输入:root = [4,2,6,1,3]
输出:1
示例 2:
输入:root = [1,0,48,null,null,12,49]
输出:1
代码:
# 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: def getMinimumDifference(self, root: TreeNode) -> int: res = [] def list(root): if not root : return None if root.left: list(root.left) res.append(root.val) if root.right: list(root.right) return res list(root) res = sorted(res) value = 999999999 for x in range(len(res)-1): if value > abs(res[x] - res[x+1]): value = abs(res[x] - res[x+1]) return value
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)