Unique Binary Search Trees I, II

Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3


卡特兰数
题解摘抄自: http://bangbingsyb.blogspot.com/2014/11/leetcode-unique-binary-search-trees-i-ii.html
思路: Unique Binary Search Trees I
 
首先注意这里是BST而不是普通的Binary Tree,所以数字会对插入的位置有影响。这类找combination/permutation的题都需要找找规律。
 
n = 0
 
n = 1
1
 
n = 2
   1                  2
     \                /
      2            1
 
n = 3
 1           3    3      2     1
    \        /     /       / \       \
     3    2    1      1   3      2
    /     /        \                    \
   2   1          2                   3
 
 
定义f(n)为unique BST的数量,以n = 3为例:
 
构造的BST的根节点可以取{1, 2, 3}中的任一数字。
 
如以1为节点,则left subtree只能有0个节点,而right subtree有2, 3两个节点。所以left/right subtree一共的combination数量为:f(0) * f(2) = 2
 
以2为节点,则left subtree只能为1,right subtree只能为2:f(1) * f(1) = 1
 
以3为节点,则left subtree有1, 2两个节点,right subtree有0个节点:f(2)*f(0) = 2
 
总结规律:
f(0) = 1
f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-2)*f(1) + f(n-1)*f(0)


public class Solution {
    public int numTrees(int n) {
        int[] c= new int[n+1];
        c[0]=1;
        for(int nm = 1;nm<=n;nm++){
            for(int m=0;m<=nm-1;m++){
                c[nm] += c[m]*c[nm-m-1];
            }
        }
        return c[n];
    }
}

 

 

II 更能体现出这种数的架构过程

ref http://blog.csdn.net/linhuanmars/article/details/24761437

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.

For example,
Given n = 3, your program should return all 5 unique BST's shown below.

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

摘抄自 code ganker“这道题是求解所有可行的二叉查找树,从Unique Binary Search Trees中我们已经知道,可行的二叉查找树的数量是相应的卡特兰数,不是一个多项式时间的数量级,所以我们要求解所有的树,自然是不能多项式时间内完成的了。算法上还是用求解NP问题的方法来求解,也就是N-Queens中介绍的在循环中调用递归函数求解子问题。思路是每次一次选取一个结点为根,然后递归求解左右子树的所有结果,最后根据左右子树的返回的所有子树,依次选取然后接上(每个左边的子树跟所有右边的子树匹配,而每个右边的子树也要跟所有的左边子树匹配,总共有左右子树数量的乘积种情况),构造好之后作为当前树的结果返回。”

public class Solution {
    public ArrayList<TreeNode> generateTrees(int n) {
        return bfs(1,n);
    }
    public ArrayList<TreeNode> bfs(int left, int right){
        ArrayList<TreeNode> res = new ArrayList<TreeNode>();
        if(left>right){ // 这里不能写=,否则就排除了单个元素的情况
            res.add(null);
            return res;
        } 
        for(int m = left;m<=right;m++){
            ArrayList<TreeNode> list1 = bfs(left, m-1);
            ArrayList<TreeNode> list2 = bfs(m+1,right);
            for(int i=0;i<list1.size();i++){
                for(int j=0;j<list2.size();j++){
                    TreeNode root = new TreeNode(m);
                    root.left =  list1.get(i);
                    root.right =  list2.get(j);
                    res.add(root);
                }
            }
        }
        return res;
    }
}

 

posted @ 2015-06-09 10:54  世界到处都是小星星  阅读(208)  评论(0)    收藏  举报