标签:
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
卡特兰数
题解摘抄自: http://bangbingsyb.blogspot.com/2014/11/leetcode-unique-binary-search-trees-i-ii.html
“思路: Unique Binary Search Trees I
”
public class Solution { public int numTrees(int n) { int[] c= new int[n+1]; c[0]=1; for(int nm = 1;nm<=n;nm++){ for(int m=0;m<=nm-1;m++){ c[nm] += c[m]*c[nm-m-1]; } } return c[n]; } }
标签:
原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4562843.html