标签:
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
1 class Solution { 2 public: 3 int numTrees(int n) { 4 5 if(n==0||n==1) 6 { 7 return 1; 8 } 9 vector<int> f(n+1); 10 11 f[0]=1; 12 f[1]=1; 13 14 for(int i=2;i<=n;i++) 15 { 16 f[i]=0; 17 for(int j=0;j<i;j++) 18 { 19 f[i]+=f[j]*f[i-j-1]; 20 } 21 } 22 23 return f[n]; 24 } 25 }; 26
【leetcode】Unique Binary Search Trees
标签:
原文地址:http://www.cnblogs.com/reachteam/p/4251638.html