Skip to content

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

Open

Description

@rushcheyo

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);
  }
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions