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

NumTrees 一题的解法似乎有改进余地 #77

Open
rushcheyo opened this issue Nov 15, 2015 · 0 comments
Open

NumTrees 一题的解法似乎有改进余地 #77

rushcheyo opened this issue Nov 15, 2015 · 0 comments

Comments

@rushcheyo
Copy link

N 个节点的 BST 的形态为 Catalan 数的第 N 项,即 C(2n, n) / (n + 1) => (2n)! / n! / (n + 1),最后可以写成这样:

class Solution {
 public:
  int numTrees(int n) {
    long ans = 1;
    for (int i = 1; i <= n; ++i) ans += ans * n / i;
    return ans / (n + 1);
  }
};
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