Unique Binary Search Trees
Example
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3public class Solution {
/**
* @param n: An integer
* @return: An integer
*/
public int numTrees(int n) {
// write your code here
int[] count = new int[n+2];
count[0] = 1;
count[1] = 1;
for (int i = 2; i <=n ;i++ ){
for(int j = 0; j < i ; j++){
count[i] += count[j] * count[i - j - 1];
}
}
return count[n];
}
}Last updated