96. Unique Binary Search Trees

public class Solution {
    public int numTrees(int n) {
        //可以采用递归方式,也可以采用线性规划,进行优化
        List<Integer> results=new ArrayList<Integer>();
        for(int i=0;i<=n;i++)
        {
            if(i==0||i==1)
                results.add(1);
            else
            {
                int res=0;
                for(int j=0;j<=i-1;j++)
                {
                    res+=(results.get(j)*results.get(i-1-j));
                }
                results.add(res);
            }
        }            
        return results.get(n);
    }
}

 

posted @ 2016-04-03 15:37  阿怪123  阅读(116)  评论(0编辑  收藏  举报