```
public int numTrees(int n) {
int ret[] = new int[n + 1];
for (int i = 0; i <= n; i ++) {
ret[i] = 0;
}
ret[0] = 1;
for (int i = 1; i < n + 1; i ++) {
for (int j = 1; j <= i; j ++) {
ret[i] += ret[j - 1] * ret[i - j];
}
}
return ret[n];
}
```