-
Notifications
You must be signed in to change notification settings - Fork 24
/
0530-minimum-absolute-difference-in-bst.js
69 lines (55 loc) · 1.4 KB
/
0530-minimum-absolute-difference-in-bst.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 530. Minimum Absolute Difference in BST
// Easy 47%
// Given a binary search tree with non-negative values, find the minimum absolute
// difference between values of any two nodes.
// Example:
// Input:
// 1
// \
// 3
// /
// 2
// Output:
// 1
// Explanation:
// The minimum absolute difference is 1, which is the difference between 2 and 1
// (or between 2 and 3).
// Note:
// There are at least two nodes in this BST.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
const getMinimumDifference = function(root) {
function inorder(root, array) {
if (root != void 0) {
inorder(root.left, array)
array.push(root.val)
inorder(root.right, array)
}
return array
}
const array = inorder(root, [])
let result = Infinity
for (let i = 1, n = array.length; i < n; i++) {
result = Math.min(array[i] - array[i - 1], result)
}
return result
}
const TreeNode = require('../structs/TreeNode')
;[
[1,null,3,2], // 1
].forEach((array) => {
console.log(getMinimumDifference(TreeNode.from(array)))
})
// Solution:
// 因为树是 BST,所以使用中序遍历就能得到一个排好序的节点数组。
// 比较数组中每个元素与上一个元素的差,选择差最小的一个。
// Submission Result: Accepted