Topic link: LeeCode96 binary search tree with different depths.
Topic description:
permutation and combination problem, such as the example in the title. When 1 is used as the root node, there are 2 below, and 3 is actually the number of combinations of two nodes, which is 2, so 3 The number of node combinations is equal to dp[0]*dp[2]+dp[1]*dp[1]+dp[2]*dp[0] (permutation and combination)
public int numTrees(int n) {
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=1;
dp[2]=2;
for (int i = 3; i < dp.length; i++) {
for (int j = 0; j < i; j++) {
dp[i]+=dp[i-j-1]*dp[j];
}
}
return dp[n];
}