int numTrees(int n) {
vector<int> dp(n + 1, 0);
dp[0] = dp[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i  1  j];
}
}
return dp[n];
}
Simple C++ DP solution

@ravi007 dp[i] represents the number of structurally unique trees of i nodes. For example, we have four nodes now, and we have already known the number of trees of 0, 1, 2, 3 nodes(we have already calculated dp[0], dp[1], dp[2], dp[3]). Then how do we get the dp[4]? We can first take 1 node out, and let's call it 'root', then we have 3 nodes left. After that, we can create a new unique structurally unique tree by putting a tree 0nodetree on left side of 'root' and putting a 3nodetree on right side of it. And how many ways are there for us to do this? The answer is dp[0] * dp[3]. For the same reason, we can put 1nodetree on the left and 2nodetree on the right, 2nodetree on the left and 1nodetree on the right, 3nodetree on the left and 0nodetree on the right. That's all.
Thus dp[4] = d[0] * dp[3] + dp[1] * d[2] + dp[2] * dp[1] + dp[3] * dp[0].
I hope I've made myself understood :)
