Saturday, November 14, 2015

[leetcode] Unique Binary Search Trees

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Solution {
    public int numTrees(int n) {
        if (n < 0) return 0;
        if (n == 0) return 1;
        int possible[] = new int[n+1];
        possible[0] = 1;
        possible[1] = 1;
        for (int i = 2; i <= n; i++){
            for (int j = 0; j < i; j++){
                possible[i] += (possible[j]*possible[i-j-1]);
            }
        }
        
        return possible[n];
    }
}

No comments:

Post a Comment