Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

96. 不同的二叉搜索树 #24

Open
AmelloAster opened this issue Jul 15, 2020 · 0 comments
Open

96. 不同的二叉搜索树 #24

AmelloAster opened this issue Jul 15, 2020 · 0 comments
Labels
Finished 经验+1 Leetcode daily topic 每日药丸 Middle 哥布林 金金 Code is life

Comments

@AmelloAster
Copy link
Collaborator

96. 不同的二叉搜索树

给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种?

示例:

输入: 3
输出: 5
解释:
给定 n = 3, 一共有 5 种不同结构的二叉搜索树:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

解题代码

var numTrees = function(n) {
 let c = 1;
 for (let i = 0; i< n ; i++) {
     c = c * 2 * (2 * i +1) / (i + 2)
 }
 return c;
};

解题思路

卡塔兰数 数学公式

代码效率

执行结果:
通过
显示详情
执行用时:
68 ms, 在所有 JavaScript 提交中击败了61.98%的用户
内存消耗:
31.6 MB, 在所有 JavaScript 提交中击败了100.00%的用户
@AmelloAster AmelloAster added Finished 经验+1 Leetcode daily topic 每日药丸 Middle 哥布林 金金 Code is life labels Jul 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Finished 经验+1 Leetcode daily topic 每日药丸 Middle 哥布林 金金 Code is life
Projects
None yet
Development

No branches or pull requests

1 participant