题解 | #二叉搜索树最小差值#

二叉搜索树最小差值

http://www.nowcoder.com/practice/f8ac976b49bd450887b9281f315186c7

中序遍历后,求数组间差值

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @return int整型
#
class Solution:
    def minDifference(self , root: TreeNode) -> int:
        # write code here
        order_list = []
        def in_order(root):
            nonlocal order_list
            if not root:
                return
            in_order(root.left)
            order_list.append(root.val)
            in_order(root.right)
        in_order(root)
        diff = float("inf")
        for i in range(1, len(order_list)):
            diff = min(diff, order_list[i] - order_list[i - 1])
        return diff
全部评论

相关推荐

12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务