Middle-题目12:96. Unique Binary Search Trees

本文探讨了如何计算n个节点的不同二叉搜索树的数量,等价于计算Catalan数,并提供了一种直接查找解决方案。

题目原文:
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个节点的二叉排序树有几种?
题目分析:
等价于求Catalan数。(证明我也不知道。求大神给出证明。)
源码:(language:java)

public class Solution {
    public int numTrees(int n) {
        int[] catalan=new int[]{1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190};
        return catalan[n];
    }
}

成绩:
0ms,beats 2.75%,众数0ms,92.25%.
Cmershen的碎碎念:
关于Catalan数的性质见https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值