-
Notifications
You must be signed in to change notification settings - Fork 25
/
0783-minimum-distance-between-bst-nodes.js
87 lines (77 loc) · 2.57 KB
/
0783-minimum-distance-between-bst-nodes.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 783. Minimum Distance Between BST Nodes
// Easy 52%
// Given a Binary Search Tree (BST) with the root node root, return the minimum
// difference between the values of any two different nodes in the tree.
// Example :
// Input: root = [4,2,6,1,3,null,null]
// Output: 1
// Explanation:
// Note that root is a TreeNode object, not an array.
// The given tree [4,2,6,1,3,null,null] is represented by the following diagram:
// 4
// / \
// 2 6
// / \
// 1 3
// while the minimum difference in this tree is 1, it occurs between node 1 and
// node 2, also between node 3 and node 2.
// Note:
// The size of the BST will be between 2 and 100.
// The BST is always valid, each node's value is an integer, and each node's
// value is different.
// This question is the same as 530:
// https://leetcode.com/problems/minimum-absolute-difference-in-bst/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
const minDiffInBST = function(root) {
function midTraverse(r, array) {
if (r) {
midTraverse(r.left, array)
array.push(r.val)
midTraverse(r.right, array)
}
}
const array = []
midTraverse(root, array)
let res = Infinity
for (let i = array.length - 1; i > 0; i--)
res = Math.min(res, array[i] - array[i - 1])
return res
}
const inorderTraversal = function(root) {
let res = Number.MAX_VALUE, pre = null
function trave(root) {
if (root.left !== null) trave(root.left)
if (pre != null) res = Math.min(res, root.val - pre)
pre = root.val
if (root.right !== null) trave(root.right)
}
trave(root)
return res
}
const TreeNode = require('../structs/TreeNode')
;[
[4,2,6,1,3,null,null], // 1
[40,20,60,10,31,null,null], // 9
].forEach((array) => {
console.log(minDiffInBST(TreeNode.from(array)))
console.log(inorderTraversal(TreeNode.from(array)))
})
// Solution:
// 1. 思路:递归每个一个节点,计算并返回其左右子树返回的值与当前值与其左右子节点的值的差的最小值。
// 【错误】因为很深的节点可能和根节点间存在最小值。
// 2. 使用中序遍历得到递增数组
// 遍历递增数组的到最小间距。
// 3. 无需数组的解法
// 中序遍历时,除了记录结果外,还要记录前一个遍历到的节点的值,用于计算与当前值的差。
// Submission Result: Accepted