中序遍历后,求数组间差值
# 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