less than 1 minute read

530. Minimum Absolute Difference in BST (easy)

class Solution {
    public int getMinimumDifference(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        getMinimumDifferenceDFS(root, list);
        list.sort(Integer::compareTo);
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < list.size() - 1; i++) {
            res = Math.min(res, list.get(i + 1) - list.get(i));
        }
        return res;
    }

    public void getMinimumDifferenceDFS(TreeNode root, List<Integer> list) {
        if (root == null)
            return;
        list.add(root.val);
        getMinimumDifferenceDFS(root.left, list);
        getMinimumDifferenceDFS(root.right, list);
    }
}