LeetCode题解——Unique Binary Search Trees II

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

想法一:递归,思想见Unique Binary Search Trees

class Solution {
public:
    vector<TreeNode*> generateTrees(int n) {
        return generateTrees(1,n);
    }
    vector<TreeNode*> generateTrees(int i, int j) {//建立sequence i,i+1,...,j的BST
      vector<TreeNode*> temp;
      TreeNode* root=NULL;
        if(i>j){
            temp.push_back(root);
            return temp;
        }
        if(i==j){
            root = new TreeNode(i);
            temp.push_back(root);
            return temp;
        }
        for(int m = i;m<=j;m++){
            vector<TreeNode*> left = generateTrees(i,m-1);
            vector<TreeNode*> right = generateTrees(m+1,j);
            for(int l = 0;l<left.size();l++){
                for(int r = 0; r<right.size();r++){
					TreeNode* root = new TreeNode(m);
                    root->left = left[l];
                    root->right = right[r];
                    temp.push_back(root);
                }
            }
        }
        return temp;
    }
};

思想二:DP ;

The basic idea is that we can construct the result of n node tree just from the result of n-1 node tree. Here's how we do it: only 2 conditions: 1) The nth node is the new root, so newroot->left = oldroot; 2) the nth node is not root, we traverse the old tree, every time the node in the old tree has a right child, we can perform: old node->right = nth node, nth node ->left = right child;and when we reach the end of the tree, don't forget we can also add the nth node here. One thing to notice is that every time we push a TreeNode in our result, I push the clone version of the root, and I recover what I do to the old node immediately. This is because you may use the old tree for several times.

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    TreeNode * clone(TreeNode * root){
        if(NULL == root) return NULL;
        TreeNode * newRoot = new TreeNode(root->val);
        newRoot->left = clone(root->left);
        newRoot->right = clone(root->right);
        return newRoot;
    }
    void addToLeft(TreeNode * oldRoot, TreeNode * newRoot, vector<TreeNode *> & results){
        TreeNode * cloneRoot = clone(oldRoot);
        newRoot->left = cloneRoot;
        results.push_back(newRoot);
    }
    void addRight(TreeNode * oldRoot, TreeNode * cur, TreeNode * newNode, vector<TreeNode *> & results){
        TreeNode *cloneRoot = clone(oldRoot);
        TreeNode *newCur = cloneRoot;
        while(NULL != newCur){
            if(newCur->val == cur->val) break;
            else newCur = newCur->right;
        }
        assert(NULL != newCur);
        TreeNode * temp = newCur->right;
        newCur->right = newNode;
        newNode->left = temp;
        results.push_back(cloneRoot);
    }
public:
    vector<TreeNode *> generateTrees(int n) {
        // DP:
        // for n-1 to n , for each n -1
        // 1) left to n
        // 2) for each right child down
        //    n replace right and right will be n left
        vector<TreeNode *> results;
        vector<TreeNode *> preResults(1, NULL);
        for(int i = 1; i <=n; i ++){
            for(int j = 0; j < preResults.size(); j++){
                // add n-1 left to n 
                TreeNode * oldRoot = preResults[j];
                TreeNode * newRoot = new TreeNode(i);
                addToLeft(oldRoot, newRoot, results);
                TreeNode * cur = oldRoot;
                while(NULL != cur){
                    TreeNode *newNode = new TreeNode(i);
                    addRight(oldRoot, cur, newNode, results);
                    cur = cur->right;
                }
            }
            swap(results, preResults);
            results.clear();
        }
        return preResults;
    }
};



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值