zoukankan      html  css  js  c++  java
  • LeetCode 95. Unique Binary Search Trees II

    Given an integer 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
    

    分析

    这道题的思路一开始想错了,参考了discussion,当n=m时的结果很容易想到应该是在n=m-1时的结果上得到的,相当于在n=m-1的各棵树插入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:
            TreeNode* clone(TreeNode* root){
                if(root == nullptr)
                    return nullptr;
                TreeNode* newroot = new TreeNode(root->val);
                newroot->left = clone(root->left);
                newroot->right = clone(root->right);
                return newroot;
            }
            vector<TreeNode *> generateTrees(int n) {
                vector<TreeNode*> r;
                if(n==0) return r;
                vector<TreeNode *> res(1,nullptr);
                for(int i = 1; i <= n; i++){
                    vector<TreeNode *> tmp;
                    for(int j = 0; j<res.size();j++){
                        TreeNode* oldroot = res[j];
                        TreeNode* root = new TreeNode(i);
                        TreeNode* target = clone(oldroot);
                        root->left = target;
                        tmp.push_back(root);
                        if(oldroot!=nullptr){
                            TreeNode* tmpold = oldroot;
                            while(tmpold->right!=nullptr){
                                TreeNode* nonroot = new TreeNode(i);
                                TreeNode *tright = tmpold->right;
                                tmpold->right = nonroot;
                                nonroot->left = tright;
                                TreeNode *target = clone(oldroot);
                                tmp.push_back(target);
                                tmpold->right = tright;
                                tmpold = tmpold->right;
                            }
                            tmpold->right = new TreeNode(i);
                            TreeNode *target = clone(oldroot);
                            tmp.push_back(target);
                            tmpold->right = nullptr;
                        }
                    }
                    res=tmp;
                }
                return res;
            }
        };
    
  • 相关阅读:
    Vue系列:.sync 修饰符的作用及使用范例
    Vue系列:Websocket 使用配置
    Vue系列:Slot 插槽的使用范例
    Vue系列:滚动页面到指定位置实现
    Vue系列:为不同页面设置body背景颜色
    Element UI系列:Upload图片自定义上传
    Vue系列:wangEditor富文本编辑器简单例子
    Element UI系列:Select下拉框实现默认选择
    sublime text 3 15个常用插件介绍
    基于iis下 wcf接口发布
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/10041240.html
Copyright © 2011-2022 走看看