中序遍历,右下持续递增,判断当前值和上一个值的差值即可
var getMinimumDifference = function(root) {
let ans = Number.MAX_SAFE_INTEGER, pre = -1;
const dfs = (root) => {
if (root === null) {
return;
}
dfs(root.left);
if (pre == -1) {
pre = root.val;
} else {
ans = Math.min(ans, root.val - pre);
pre = root.val;
}
dfs(root.right);
}
dfs(root);
return ans;
};js
网友评论