Unique Binary Search Trees
Description
Given n, how many structurally unique BSTs (binary search trees) that store values 1…n?
public class Solution {
/**
* @param n: An integer
* @return: An integer
*/
public int numTrees(int n) {
// write your code here、
if(n == 0 ||n == 1){
return 1;
}
int[] C = new int[n+1];
C[0] = 1;
for(int num=1;num<=n;num++){
for(int i=0;i<=num-1;i++){
C[num] += C[i]*C[(num-1)-i];
}
}
return C[n];
}
}
独特二叉搜索树的数量
145

被折叠的 条评论
为什么被折叠?



