算法分析与设计第五周习题:95. Unique Binary Search Trees II

与96题的对比:
95题是96题Unique Binary Search Trees的升级版,95题增加了构建出所有树的要求,而不仅仅是计算树的数量。对于96题,我们可以轻易得出树数量的规律。即:

1.长度为3的树可由根和一个长度为2的子树构成(即以3为根或者以1为根) 或者 由根和两个长度为1的子树构成(
即以2为根)
2. 长度为4的子树可由根和一个长度为3的子树构成(即以4或者1为根) 或者 由根和一个长度为1的子树和一个长度为2的子树构成(即以2为根或者3为根)
3. 可以发现,长度为n的树的数量取决于它能由多少种不同形式的子树构成。所以以此类推,便可得到递推公式。


以count作为分割左右子树的位置,即可得到以下的递推代码。

for count in range(i - 1, mid - 1, -1):
    sum += nums[count] * nums[i - count - 1]

回到95题,既然题目要求我们生成N棵子树,那么我们首先想到的必是先拷贝N-1棵子树,以root为新树的根,用roott源树的根进行拷贝。

注意:新树的跟必须要使用&引用值,因为root根一开始还未赋值,是为定义的值,需要在递归的过程赋值。若此处不使用&引用,则源程序在调用buildTree时,会给参数root拷贝一份源程序中root的值,而此时的root是未定义的,所以程序会报错。

TreeNode* buildTree(TreeNode*&root, TreeNode* roott) {
        if (!roott)
            return NULL;
        root = new TreeNode(roott->val);
        root->left = buildTree(root->left, roott->left);
        root->right = buildTree(root->right, roott->right);
        return root;
    }
 拷贝完N-1棵子树后,自然是将新的值value放入前面已经构建完成的树中。例如,前面已经构建出 [2,1,NULL] 和 [1,NULL,2] 两棵树,那么当n=3的时候,我们需要将3插入这两棵树中。
考虑到,我们是根据n值从小到大进行构建的,所以n值只能插入原树的右子树中,或者以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) {
        if (n == 0)
            return lastLevel;
        if (n == 1)
        {
            TreeNode* newNode = new TreeNode(1);
            lastLevel.push_back(newNode);
            return lastLevel;
        }
        TreeNode* root = new TreeNode(1);
        lastLevel.push_back(root);
        for (int i = 2; i < n + 1; ++i)
        {
            vector<TreeNode*> newLevel;
            for (int root = 0; root < lastLevel.size(); ++root)
            {
                TreeNode* newNode = new TreeNode(i);
                newNode->left = lastLevel[root];
                newLevel.push_back(newNode);
                //查找根节点右子树的高度
                int height = 0;
                TreeNode* temp = lastLevel[root];
                while (temp->right != NULL) {
                    height++;
                    temp = temp->right;
                }
                //建立height + 1个新树,因为一个节点有两个插入位置
                for (int h = 0; h < height + 1; h++)
                {
                    TreeNode* newTreeNode = buildTree(newTreeNode, lastLevel[root]);
                    getChild(newTreeNode, i, h);
                    newLevel.push_back(newTreeNode);
                }
            }
            lastLevel = newLevel;
            if (i == n) {
                return lastLevel;
            }
        }

    }
private:
    vector<TreeNode*> lastLevel;
    vector<TreeNode*> newLevel;

    TreeNode* buildTree(TreeNode*&root, TreeNode* roott) {
        if (!roott)
            return NULL;
        root = new TreeNode(roott->val);
        root->left = buildTree(root->left, roott->left);
        root->right = buildTree(root->right, roott->right);
        return root;
    }

    void getChild(TreeNode* root, int value, int height) {
        TreeNode *temp = root, *father = root;
        while (height >= 0) {
            father = temp;
            temp = temp->right;
            height--;
        }
        TreeNode* newNode = new TreeNode(value);
        father->right = newNode;
        newNode->left = temp;
    }
    void inOrder(TreeNode* root) {
        if (root) {
            inOrder(root->left);
            cout << root->val << ' ';
            inOrder(root->right);
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值