leet530:二叉搜索树的最小绝对差
程序员文章站
2022-04-24 20:45:57
...
static TreeNode pre = null;
static int res = Integer.MAX_VALUE;
public static int getMinimumDifference(TreeNode root) {
inOrder(root);
return res;
}
public static void inOrder(TreeNode root) {
if(root == null) {
return;
}
inOrder(root.left);
if(pre != null) {
//res = Math.min(res, root.val - pre.val);
res = Math.min(res, Math.abs(root.val - pre.val));
}
pre = root;
inOrder(root.right);
}