二叉搜索树的最小绝对差

Desc

给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var getMinimumDifference = function(root) {
const arr = [];

const inorder = (node) => {
if (node){
inorder(node.left);
arr.push(node.val);
inorder(node.right);
}
}

inorder(root);

let minDiffValue = Number.MAX_SAFE_INTEGER;

for (let i = 0; i < arr.length - 1; i++){
minDiffValue = Math.min(minDiffValue, arr[i + 1] - arr[i]);
}

return minDiffValue;

};