-
Notifications
You must be signed in to change notification settings - Fork 25
/
0096-unique-binary-search-trees.js
71 lines (58 loc) · 1.75 KB
/
0096-unique-binary-search-trees.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
// 96. Unique Binary Search Trees
// Medium 41%
// Given n, how many structurally unique BST's (binary search trees) that store
// values 1...n?
// For example,
// Given n = 3, there are a total of 5 unique BST's.
// 1 3 3 2 1
// \ / / / \ \
// 3 2 1 1 3 2
// / / \ \
// 2 1 2 3
/**
* @param {number} n
* @return {number}
*/
const numTrees = function(n) {
if (n <= 0) return 0
const res = [1, 1] // in order to elegant, set res[0] = 1
for (let i = 2; i <= n; i++) {
res[i] = 0
for (let left = 0; left < i; left++) {
const right = i - left - 1
res[i] += res[left] * res[right]
}
}
return res[n]
}
;[
0, // 0
1, // 1
2, // 2
3, // 5
4, // 14
5, // 42
6, // 132
].forEach(n => {
console.log(numTrees(n))
})
// Solution:
// 0 0
// 1 1
// 2 (1*1)+(1*1)=2
// 3 (1*2)+(1*1)+(2*1)=5
// 4 (1*5)+(1*2)+(2*1)+(5*1)=14
// 5 (1*14)+(1*5)+(2*2)+(5*1)+(12*1)=38
// 6 (1*38)+(1*12)+(2*5)+(5*2)+(12*1)+(38*1)=132
// ...
// 这与计算斐波那契数是类似的问题,不能使用递归,这样会出现大量重复计算。
// 于是也和生成斐波那契数一样,使用动态规划。
// 假设有6个节点
// 在 i=6 的迭代过程中,每个数字都可以作为根节点,
// 以下是以 3 为根节点时,能够构造的不同的树的解释。
// O O @ O O O
// \ / | \ /
// 左子树 根 右子树
// F(2)=2 F(3)=5
// F(2) * F(3) = 2 * 5 = 10
// Submission Result: Accepted