Middle-题目12:96. 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个节点的二叉排序树有几种?
题目分析:
等价于求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。

你可能感兴趣的:(Middle-题目12:96. Unique Binary Search Trees)