0MS C solution which is very easy to understand


  • 0
    B
    int num[1000];
    
    int go(int begin , int end){
        if (begin >= end) return 1;
        if (num[end - begin + 1]) return num[end - begin + 1];
        int tmp = 0;
        for (int i = begin;i <= end;i++){
            tmp += (go(i + 1 , end) * go(begin , i - 1)); //multiply
        }
        return num[end - begin + 1] = tmp;
    }
    
    int numTrees(int n) {
        memset(num , 0 ,sizeof(num));
        return go(1 , n);
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.