java/Tree/MiniumumDistanceBetweenBSTNodes.java
<filename>java/Tree/MiniumumDistanceBetweenBSTNodes.javapublic class MiniumumDistanceBetweenBSTNodes {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { 
            this.val = val; 
        }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static Integer res = Integer.MAX_VALUE;
    public static Integer pre = null;
    public static int minDiffInBST(TreeNode root) {
        if (root.left != null) minDiffInBST(root.left);
        if (pre != null) res = Math.min(res, root.val - pre);
        pre = root.val;
        if (root.right != null) minDiffInBST(root.right);
        return res;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        int minDiff = minDiffInBST(root);
        System.out.println("Minimum Distance Between BST Nodes: " + minDiff);
    }
}
