-----QUESTION-----
Given
For example,
Given
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
-----SOLUTION-----
class Solution {
public:
int numTrees(int n) {
return dfs(1,n);
}
int dfs(int start, int end)
{
int leftNum = 0;
int rightNum = 0;
int num = 0;
for ( int i = start; i <= end; i++)
{
if(i!=start) leftNum = dfs(start,i-1);
else leftNum = 1;
if(i!=end) rightNum = dfs(i+1,end);
else rightNum = 1;
num += leftNum*rightNum;
}
return num;
}
};