add: 不同的二叉搜索树
This commit is contained in:
parent
2e60e7aa98
commit
20f821a921
13
.vscode/settings.json
vendored
13
.vscode/settings.json
vendored
@ -36,5 +36,16 @@
|
||||
"zhan",
|
||||
"zhong",
|
||||
"zhuan"
|
||||
]
|
||||
],
|
||||
"standard.options": {
|
||||
"globals": [
|
||||
"$",
|
||||
"jQuery",
|
||||
"fetch"
|
||||
],
|
||||
"usePackageJson": true,
|
||||
"env": {
|
||||
"jest": true
|
||||
}
|
||||
}
|
||||
}
|
@ -516,6 +516,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 108. 将有序数组转换为二叉搜索树 <https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/>
|
||||
- LintCode 106. 有序链表转换为二叉搜索树 <https://www.lintcode.com/problem/convert-sorted-list-to-binary-search-tree/description>
|
||||
|
||||
- [不同的二叉搜索树](src/tree/unique-binary-search-trees.js)
|
||||
|
||||
- LeetCode 96. 不同的二叉搜索树 <https://leetcode-cn.com/problems/unique-binary-search-trees/>
|
||||
|
||||
## 链表
|
||||
|
||||
- [合并 K 个排序链表](src/list/merge-k-sorted-lists.js)
|
||||
|
12
src/tree/unique-binary-search-trees.js
Normal file
12
src/tree/unique-binary-search-trees.js
Normal file
@ -0,0 +1,12 @@
|
||||
// 卡塔兰数
|
||||
/**
|
||||
* @param {number} n
|
||||
* @return {number}
|
||||
*/
|
||||
export const numTrees = function (n) {
|
||||
let C = 1
|
||||
for (let i = 0; i < n; ++i) {
|
||||
C = C * 2 * (2 * i + 1) / (i + 2)
|
||||
}
|
||||
return C
|
||||
}
|
5
test/tree/unique-binary-search-trees.test.js
Normal file
5
test/tree/unique-binary-search-trees.test.js
Normal file
@ -0,0 +1,5 @@
|
||||
import { numTrees } from '../../src/tree/unique-binary-search-trees'
|
||||
|
||||
test('不同的二叉搜索树', () => {
|
||||
expect(numTrees(3)).toBe(5)
|
||||
})
|
Loading…
Reference in New Issue
Block a user