Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
webVueBlog opened this issue Sep 4, 2022 · 0 comments
Open

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

webVueBlog opened this issue Sep 4, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

96. 不同的二叉搜索树

Description

Difficulty: 中等

Related Topics: , 二叉搜索树, 数学, 动态规划, 二叉树

给你一个整数 n ,求恰由 n 个节点组成且节点值从 1n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。

示例 1:

输入:n = 3
输出:5

示例 2:

输入:n = 1
输出:1

提示:

  • 1 <= n <= 19

Solution

Language: JavaScript

/**
 * @param {number} n
 * @return {number}
 */
// 数学
var numTrees = function(n) {
    let res = 1
    for (let i = 1; i < n; i++) {
        res = res * 2 * (2 * i + 1) / (i + 2)
    }
    return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant