LeetCode: Unique Binary Search Trees I & II

Title:

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

首先注意这里是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)
 
使用动态规划的代码
class Solution {
public:
    int numTrees(int n) {
        vector<int> v(n+1,0);
        v[0] = 1;
        v[1] = 1;
        for (int i = 2; i <= n; i++){
            for (int k = 0; k < i; k++){
                v[i] += v[k] * v[i-1-k];
            }
        }
        return v[n];
    }
};

Title:

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

 

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

 

要求生成所有的unique BST,类似combination/permutation的题目,可以递归构造。
 
1. 根节点可以任取min ~ max (例如min  = 1, max = n),假如取定为i。
2. 则left subtree由min ~ i-1组成,假设可以有L种可能。right subtree由i+1 ~ max组成,假设有R种可能。生成所有可能的left/right subtree。
3 对于每个生成的left subtree/right subtree组合<T_left(p), T_right(q)>,p = 1...L,q = 1...R,添加上根节点i而组成一颗新树。
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> generateTrees(int n) {
        return fun(1,n);
    }
    vector<TreeNode*> fun(int min,int max){
        vector<TreeNode*> ret;
        if (min > max){
            ret.push_back(NULL);
            return ret;
        }
        for(int i = min; i <= max; i++){
            vector<TreeNode*> left = fun(min,i-1);
            vector<TreeNode*> right = fun(i+1, max);
            for (int m = 0; m < left.size(); m++){
                for(int n = 0; n <right.size(); n++){
                    TreeNode * root = new TreeNode(i);
                    root->left = left[m];
                    root->right = right[n];
                    ret.push_back(root);
                }
            }
        }
return ret; } };

 

posted on 2015-05-10 18:49  月下之风  阅读(203)  评论(0编辑  收藏  举报

导航