LeetCode530. 二叉搜索树的最小绝对差

class Solution {
    int min = Integer.MAX_VALUE;
    TreeNode pre = null;
    public int getMinimumDifference(TreeNode root) {
        inOrder(root);
        return min;
    }
    private void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        if (pre != null) {
            min = Math.min(min, root.val - pre.val);
        }
        pre = root;
        inOrder(root.right);
    }
}

 

posted @ 2020-12-25 17:02  不学无墅_NKer  阅读(40)  评论(0编辑  收藏  举报