DP solution - Java


  • 4
    C
      public int numTrees(int n) {
           
            int[] tree = new int[n+1];
            tree[0]=1;
            tree[1]=1;
            for(int i=2; i<=n; i++){
                int sum=0;
                for(int j=1; j<=i; j++){
                    sum=sum+tree[j-1]*tree[i-j];
                }
                tree[i]=sum;
            }
            return tree[n];
        }

  • 0
    L

    what does tree[i-j] mean?

    follow up: I think I got it. thanks for your solution anyway


  • 0
    V

    Could you please explain the logic of your solution in detail as possible ? I would like to understand the algorithm.

    Thanks,
    Vignesh


Log in to reply
 

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