Leetcode Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.

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

题目的的意思是给出n个节点,求出有多少不同的二叉树,实际是计算卡特兰数
可以参考
二叉树卡特兰数拉特兰数的计算

卡特兰数的计算公式是


迭代或者递归实现都可以
class Solution {
public:
    int catalan(int n){
        if(n == 1) return 1;
        else return 2*(2*n-1)*catalan(n-1)/(n+1);
    }
    
    int numTrees(int n) {
        return catalan(n);
    }
};
 
原文地址:https://www.cnblogs.com/xiongqiangcs/p/3819017.html