leetcode 95 & 96 解题思路

95. Unique Binary Search Trees II

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

Example:

Input: 3
Output:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:

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

生成n个节点的所有二叉搜索树。这里跟二叉搜索树一样也是用到了递归进行建树。

/**
 * 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) {
        vector<TreeNode*> temp;
        if(n <= 0) 
            return temp;
        return generate(1, n);
    }
    
    vector<TreeNode*> generate(int left, int right)  //递归建立左右子树
    {
        vector<TreeNode*> res;
        if(left > right)             
        {   
            res.push_back(NULL);
        } 
        
        for(int k = left; k <= right; k++)   //k为当前对应情况的根节点
        {
            vector<TreeNode*> Ltree = generate(left, k - 1);   // 建立左子树
            vector<TreeNode*> Rtree = generate(k + 1, right);  // 建立右子树

            for(int i = 0; i < Ltree.size(); i++)    //子树有多种组合的情况
            {
                for(int j = 0; j < Rtree.size(); j++)
                {
                    TreeNode* cur = new TreeNode(k);  
                    cur->left = Ltree[i];            // 以Ltree[i]为左子树的根节点          
                    cur->right = Rtree[j];           // 以Ltree[i]为右子树的根节点
                    res.push_back(cur);              // 保存根节点
                }
            }
        }
        return res;     
    }
};

 

 

96. Unique Binary Search Trees

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

Example:

Input: 3
Output: 5
Explanation:
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

求 n 个节点 可以组合成多少种二叉搜索树。

利用迭代, l0 个节点只有一种情况, 1个节点也只有一种情况,2个节点有两种情况,3个节点有5种情况,即左右子节点分别为 (2,0),(1 ,1)和(0,2)。还需要注意的一点是,求左右子树所有的个数时根据排列组合原则是相乘的关系。

可以得出规律:  f(0) = 1

                           f(1) = 1

                           f(2) = f(1) * f(0) + f(0) * f(1)

                           f(3) = f(2)*f(0) + f(1)*f(1) + f(0)*f(2)

                           f(n) = f(n - 1) * f(0) + f(n - 2) * f(1) + ... + f(0) * f(n - 1)

其实这就是 卡特兰数

class Solution {
public:
    int numTrees(int n) {
        if( n == 0 )
            return 1;
        if( n == 1 )
            return 1;
        int* num = new int[n + 1];
        num[0] = 1;
        num[1] = 1;
        
        for(int i = 2; i <= n; i++)        
        {
            num[i] = 0;
            for(int j = 0; j <  i; j++)
                num[i] = num[i] +  num[j] * num[i - j - 1];
        }
        return num[n];
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值