1. Question: Given a positive number, return the number of unique sorted binary trees consisting of [1, n] n numbers.
2. Idea: Use dynamic programming to calculate: For example, when we are calculating how many different compositions of the four numbers 1-4, all four numbers 1-4 can be used as the root node, there are the following four situations :
We assume that the function we need is F(x), then F(0) = F(1) = 1, F(2) = 2, F(3) = 5 [you can test it yourself]
① When 1 is the root node When: then the remaining elements can only be on the right side: then the only number of the right subtree is the number when n is three: F(3)
② When 2 is the root node: then 1 can only be in the On the left, the remaining 3 and 4 can only be on the right, there is only one composition on the left, and there are F(2) compositions on the right.
③ When 3 is the root node, there is only 4 on the right side, and 1 and 2 on the left side, the same as when 2 is the root node.
④ When 4 is the root node, the remaining elements can only be all on the left, so there are F(3) ways of composition.
So the total composition is the sum of the above 4 cases.
As you can see above, the substructure is used repeatedly, so we can use dynamic programming to do this problem:
int numTrees(int n) {
int size = n+1;
vector<int> target(size, 1);
for (int i = 2; i < size; ++i) {
int current = target[i-1] << 1; //①
for (int j = 2; j < i; ++j) {
current += target[j-1] * target[i-j]; //②
}
target[i] = current;
}
return target[n];
}
Two points to explain above:
① Why multiply by 2? Because when we are calculating the nth number, the case where 1 is the root and n is the root is the same. Here I put these two cases out, so the following j starts from 2.
② This inner loop is used to superimpose how many different situations there are from 2 to n-1 as the root.
Finally, assign values to the array to get the number of non-repeated sorted binary trees composed of these numbers [1, n].