530. 二叉搜索树的最小绝对差
小于 1 分钟
530. 二叉搜索树的最小绝对差简单
解法:中序遍历
class Solution {
int res = Integer.MAX_VALUE;
TreeNode last = null;
public int getMinimumDifference(TreeNode root) {
if (root.left != null) {
getMinimumDifference(root.left);
}
res = last == null ? res : Math.min(res, Math.abs(last.val - root.val));
last = root;
if (root.right != null) {
getMinimumDifference(root.right);
}
return res;
}
}
Powered by Waline v2.15.5