Unique Binary Search Trees [LeetCode]

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

Summary: dynamic programming

     int numTrees(int n) {
vector<int> nums;
nums.push_back();
for(int i = ; i <= n; i ++) {
if(i == ){
nums.push_back();
}else if(i == ){
int num = i * nums[i - ];
nums.push_back(num);
}else if(i >= ){
int num = ;
for(int j = ; j <= i; j ++)
num += (nums[j - ] == ? : nums[j - ]) * (nums[i - j] == ? : nums[i - j]);
nums.push_back(num);
}
}
return nums[n];
}
上一篇:leetcode 95 Unique Binary Search Trees II ----- java


下一篇:Unique Binary Search Trees——LeetCode