Java实现-不同的二叉查找树1

来源:互联网 发布:微信打开淘宝 编辑:程序博客网 时间:2024/06/07 11:19

给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?

样例

给出n = 3,有5种不同形态的二叉查找树:

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


public class Solution {    /**     * @paramn n: An integer     * @return: An integer     */    public int numTrees(int n) {        // write your code here       if(n==0)return 1;long count=1;for(int i=1;i<=n;i++){count=(4*i-2)*count/(i+1);}return (int)count;    }}



原创粉丝点击