import java.util.*;

public class Solution {
    public int minDifference (TreeNode root) {
        // 初始化
        List<Integer> list = new ArrayList<>();
        // 获取中序遍历序列
        inOrder(root, list);
        // 计算所有相邻结点的差值
        int[] diffs = new int[list.size()-1];
        for (int i = 0; i < diffs.length; i++) {
            diffs[i] = list.get(i+1) - list.get(i);
        }
        // 返回差值数组中的最小值
        Arrays.sort(diffs);
        return diffs[0];
    }

    public static void inOrder(TreeNode root, List<Integer> list) {
        // 预处理
        if (root == null) return;
        // 左根右
        inOrder(root.left, list);
        list.add(root.val);
        inOrder(root.right, list);
    }
}