# 如何获得两个值或JavaScript中的节点之间的差异

``````root = [4,2,6,1,3,null,null]
``````

``````1
``````

``````      4
/   \
2      6
/ \
1   3
``````

``````var minDiffInBST = function (root) {
let min = Number.MAX_VALUE
const getMin = (node) => {
if (node.left && node.right) {
console.log('both')
return Math.min(node.val - node.left.val, node.right.val - node.val)
} else if (node.right) {
console.log('right')
return node.right.val - node.val
} else if (node.left) {
console.log('left')
return node.val - node.left.val
} else {
return Number.MAX_VALUE
}
}

const preOrder = (root) => {
if (!root) {
return 0;
}
let x = getMin(root)
if (x < min)
min = x
preOrder(root.left)
preOrder(root.right)

}
preOrder(root)
return min
};

console.log(minDiffInBST({
"val": 90,
"left": {
"val": 69,
"left": {"val": 49, "left": null, "right": {"val": 52, "left": null, "right": null}},
"right": {"val": 89, "left": null, "right": null}
},
"right": null
}

))
``````

Getting output `3` expected output `1`

question I am taken from here https://leetcode.com/problems/minimum-distance-between-bst-nodes/