int numTrees(int n) {
     int ans=0;
     if (n==0) return 1;
     if (n<3) return n;
     for (int i=0;i<n;i++) ans+=numTrees(i)*numTrees(n-1-i);
     return ans;
 }
Exemplo n.º 2
0
 int numTrees(int n) {
     if(!n) return 1;
     if(n == 1) return 1;
     if(n == 2) return 2;
     int res = 0;
     for(int i = 0; i < n; ++i) res += numTrees(i)*numTrees(n - i - 1);
     return res;
 }
 int numTrees(int n) {
     if (n <= 1) return 1;
     int total = 0;
     for (int i = 0; i <= n - 1; i++) {
         total += (numTrees(i) * numTrees(n - 1 - i));
     }
     return total;
 }
 // recursion
 int numTrees(int n) {
     if(n<2) return 1;
     int res=0;
     for(int i=0; i<n; ++i){
         res+=numTrees(i)*numTrees(n-1-i);
     }
     return res;
 }
 int numTrees(int n) {
     if (n <= 1) return 1;
     int res = 0;
     for (int i = 0; i <= n-1; i++) {
         res += numTrees(i) * numTrees(n-1-i);
     }
     
     return res;
 }
  int numTrees(int n) {
 if (n == 0) return 1;
 int sum = 0;
 for (int i = 0; i < n; ++i)
 {
     sum += numTrees(i) * numTrees(n-1-i);
 }
 return sum; 
      
  }
 int numTrees(int n) {
     int result=0;
     if(n==0) return 1;
     if(n==1) return 1;
     for(int i=0;i<n;i++)
     {
         result += numTrees(i)*numTrees(n-1-i);
     }
     return result;
 }
 int numTrees(int n) {
     if( n == 0 || n == 1 )
         return 1;
     else{
         int res = 0;
         for(int i = 0; i < n; i++  ){
             res += numTrees(i) * numTrees( n - 1 - i );
         }
         return res;
     }
 }
    int numTrees(int s, int e)
	{
		if( s > e)
			return 1;
		int trees = 0;
		for(int i = s ;i<= e;i++)
		{
			trees += numTrees(s,i-1) * numTrees(i+1,e);
		}
		return trees;
	}
 int numTrees(int n) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     if (n==0){return 1;}
     if (n==1){return 1;}
     int sum=0;
     for (int i=1;i<=n;i++){
         sum += numTrees(i-1)*numTrees(n-i);
     }
     return sum;
      
 }
Exemplo n.º 11
0
int numTrees2(int n) {
    if (n<=0) return 0;
    if (n == 1 ) return 1;
    int sum=0;
    for (int i=1; i<=n; i++){
        if (i==1||i==n){
            sum += numTrees(n-1);
        }else{
            sum += (numTrees(i-1) * numTrees(n-i));
        }
    }
    return sum;
}
Exemplo n.º 12
0
 int numTrees(int n) {
     int result = 0;
     if(n <= 0) return 0;
     if(n == 1) return 1;
     for(int i = 0; i < n; i++)
     {
         int l = numTrees(i);
         int r = numTrees(n - i - 1);
         if(!l) result += r;
         else if(!r) result += l;
         else if(l && r) result += l*r;
     }
     return result;
 }
Exemplo n.º 13
0
    int numTrees(int n) { // 0 ms
        if (n < 3) { return n; }
        if (_map.find(n) != _map.end()) { return _map[n]; }

        long long res = 0;
        for (int root = 1; root <= n; ++root) { // take turns to be root
            long long left  = numTrees(root-1);
            long long right = numTrees(n-root);
            if (left == 0 || right == 0) { res += (left+right); }
            else { res += left * right; }
        }
        _map[n] = res;

        return _map[n];
    }
Exemplo n.º 14
0
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", numTrees(n));
    return 0;
}
Exemplo n.º 15
0
 int numTrees(int n) {
     if (n==0) return 1;
     if (n==1) return 1;
     vector<int> nTree;
     nTree.push_back(numTrees(0));
     nTree.push_back(numTrees(1));
     for(int i=2;i<n+1;i++){
         int tmp;
         tmp=0;
         for(int j=0;j<i;j++){
             tmp+=nTree[j]*nTree[i-1-j];
     }
     nTree.push_back(tmp);
 }
 return nTree[n];
 }
 int numTrees(int n) {
     if(n==1)
         return 1;
     else if(n==2)
         return 2;
     else
         return (long)(4*n-2)*numTrees(n-1)/(n+1);
 }
Exemplo n.º 17
0
int main(int argc, char** argv)
{
    int n=2;
    if (argc>1){
        n = atoi(argv[1]);
    }
    printf("%d=%d\n", n, numTrees(n));
    return 0;
}
 int numTrees(int n) {
     vector<int> seq;
     int cnt = 0;
     while(cnt <= n){
         numTrees(seq, cnt);
         ++cnt;
     }
     return seq.back();
 }
Exemplo n.º 19
0
int numTrees(int n)
{
  int sum = 0;
  if (0 >= n) {
    return 1;
  }
  if (1 == n) {
    return 1;
  }

  for(int i=0; i<n; i++) {
    int cur = 1;
    cur *= numTrees(i);
    cur *= numTrees(n-i-1);
    sum += cur;
  }
  return sum;
}
int numTrees(int n) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    int* array = new int[n];
    array[0] = 1;
    for(int i = 1; i < n; i ++){
        array[i] = numTrees(i);
    }
    int ret = 0;
    for(int i = 0; i < n; i++){
        ret += array[i] * array[n-i-1];
    }
    return ret;
}
Exemplo n.º 21
0
void main()
{
	time_t start, stop;
	time(&start);
	int result = 0;

	//LSearchInt();
	//printf("%s\n", LSearchCharArray());

	//StackStringTest();

	//TestSingleNumber();

	//TestReverseInteger();

	//TestMaxProfit();

	printf("%d\n", numTrees(9));

	time(&stop);
	printf("%0.2f milliseconds. \n", (double)clock() / CLOCKS_PER_SEC * 1000);
	scanf_s("%d", &result);
}
Exemplo n.º 22
0
Arquivo: 96_bak.c Projeto: unasm/utils
int main(int argc, const char *argv[])
{
	printf("%d\n", numTrees(5))	;
	return 0;
}
Exemplo n.º 23
0
int main() {
    printf("%d\n", numTrees(4));
    return 0;
}
 int numTrees(int n) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     return numTrees(1,n);
 }
Exemplo n.º 25
0
int main() {
    cout<<numTrees(3)<<endl;
    return 0;
}
Exemplo n.º 26
0
int main()
{
  for(int i=0; i<10; i++) {
    printf("numTrees(%d) == %d\n", i, numTrees(i));
  } 
}