• LeetCode OJ——Unique Binary Search Trees II


    http://oj.leetcode.com/problems/unique-binary-search-trees-ii/

    一题要求得出所有树的种类数,二题要求得出所有树。

    在一题的基础上修改代码,还是要提前想清楚再写。

    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct TreeNode {
             int val;
             TreeNode *left;
             TreeNode *right;
             TreeNode(int x) : val(x), left(NULL), right(NULL) {}
         };
    
    class Solution {
    public:
        vector<TreeNode *> fun(int start,int end )
        {
            vector<TreeNode *> ansTemp;
            vector<TreeNode *> ansTemp2;
            vector<TreeNode *> ansTemp3;
    
            ansTemp.clear();
            if(start>end)
            {
                ansTemp.push_back(NULL);
                return ansTemp;
            }
            if(start == end)
            {
                TreeNode *mid = new TreeNode(start);
                ansTemp.push_back(mid);
                return ansTemp;
            }
        
            for(int i=start;i<=end;i++)//枚举的中间位置
            {
                ansTemp2 = fun(start,i-1);
                ansTemp3 = fun(i+1,end);
                
                if(ansTemp2.size()!=0&&ansTemp3.size()!=0)
                    for(int j = 0;j<ansTemp2.size();j++)
                    {
                        for(int k = 0;k<ansTemp3.size();k++)
                        {
                            TreeNode * root = new TreeNode(0);
                            root->val = i;
                            root->left = ansTemp2[j];
                            root->right = ansTemp3[k];
                            ansTemp.push_back(root);
                        }
                    }
                else if(ansTemp2.size()!=0&&ansTemp3.size()==0)
                    for(int j = 0;j<ansTemp2.size();j++)
                    {
                        TreeNode * root = new TreeNode(0);
                        root->val = i;
                        root->left = ansTemp2[j];
                        root->right = NULL;
                        ansTemp.push_back(root);
                    }
                else if(ansTemp2.size()==0&&ansTemp3.size()!=0)
                    for(int k = 0;k<ansTemp3.size();k++)
                    {
                        TreeNode * root = new TreeNode(0);
                        root->val = i;
                        root->left = NULL;
                        root->right = ansTemp3[k];
                        ansTemp.push_back(root);
                    }
                else
                {
                    TreeNode * root = new TreeNode(i);
                    ansTemp.push_back(root);
                }
    
            }
            
            return ansTemp;
        }
    
        vector<TreeNode *> generateTrees(int n) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            vector<TreeNode *> ans;
            ans.clear();        
            ans =  fun(1,n);
            return ans;
        }
    };
     
    int main()
    {
        Solution my;
        my.generateTrees(0);
        return 0;
    }

    亲爱的,加油。

  • 相关阅读:
    Codeforces Round #239(Div. 2) 做后扯淡玩
    hdu 3507 Print Article
    prufer序列
    POJ 2778 DNA Sequence
    Codeforces Round #237 (Div. 2)
    poj3352
    图论知识
    POJ 2186
    Codeforces Round #236 (Div. 2)
    POJ 2823 Sliding Window
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3449918.html
Copyright © 2020-2023  润新知