solution.md 5.3 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 不同的二叉搜索树 II
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<div class="notranslate">
    <p>给你一个整数 <code>n</code> ,请你生成并返回所有由 <code>n</code> 个节点组成且节点值从 <code>1</code><code>n</code> 互不相同的不同
        <strong>二叉搜索树</strong><em> </em>。可以按 <strong>任意顺序</strong> 返回答案。
    </p>

    <p>&nbsp;</p>

    <div class="original__bRMd">
        <div>
            <p><strong>示例 1:</strong></p>
            <img style="width: 600px; height: 148px;"
                src="https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg" alt="">
            <pre><strong>输入:</strong>n = 3
<strong><br />输出:</strong>[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
    </pre>

            <p><strong>示例 2:</strong></p>

            <pre><strong>输入:</strong>n = 1
<strong><br />输出:</strong>[[1]]
    </pre>

            <p>&nbsp;</p>

            <p><strong>提示:</strong></p>

            <ul>
                <li><code>1 &lt;= n &lt;= 8</code></li>
            </ul>
        </div>
    </div>
</div>
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
36

每日一练社区's avatar
每日一练社区 已提交
37
## aop
F
fix bug  
feilong 已提交
38

每日一练社区's avatar
每日一练社区 已提交
39
### before
F
fix bug  
feilong 已提交
40

每日一练社区's avatar
每日一练社区 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
```cpp
#include <bits/stdc++.h>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
```
### after
F
fix bug  
feilong 已提交
56

每日一练社区's avatar
每日一练社区 已提交
57 58 59 60 61
```cpp

```

## 答案
F
fix bug  
feilong 已提交
62

每日一练社区's avatar
每日一练社区 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
```cpp
class Solution
{
public:
    vector<TreeNode *> generateTrees(int n)
    {
        if (n <= 0)
            return vector<TreeNode *>{};
        return generate(1, n);
    }

    vector<TreeNode *> generate(int begin, int end)
    {
        vector<TreeNode *> ret;
        if (begin > end)
        {
            return ret;
        }

        for (int i = begin; i <= end; ++i)
        {

            vector<TreeNode *> lSubs = generate(begin, i - 1);
            vector<TreeNode *> rSubs = generate(i + 1, end);
            for (auto l : lSubs)
            {
                for (auto r : rSubs)
                {
                    TreeNode *root = new TreeNode(i);
                    root->left = l;
                    root->right = r;
                    ret.push_back(cloneTree(root));
                }
            }
        }

        return ret;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
105

每日一练社区's avatar
每日一练社区 已提交
106
### A
F
fix bug  
feilong 已提交
107

每日一练社区's avatar
每日一练社区 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
```cpp
class Solution
{
public:
    vector<TreeNode *> generateTrees(int n)
    {
        vector<TreeNode *> res;
        if (n < 1)
            return res;
        res = creatTree(1, n);
        return res;
    }
    vector<TreeNode *> creatTree(int start, int end)
    {
        vector<TreeNode *> res;
        if (start > end)
        {
            res.push_back(NULL);
            return res;
        }
        if (start == end)
        {
            TreeNode *node = new TreeNode(start);
            res.push_back(node);
            return res;
        }
        for (int i = start; i <= end; i++)
        {
            vector<TreeNode *> left = creatTree(start, i - 1);
            vector<TreeNode *> right = creatTree(i + 1, end);
            for (int j = 0; j < left.size(); j++)
            {
                for (int k = 0; k < right.size(); k++)
                {
                    TreeNode *node = new TreeNode(i);
                    node->left = left[j];
                    node->right = right[k];
                    res.push_back(node);
                }
            }
        }
        return res;
    }
};
```

### B
F
fix bug  
feilong 已提交
155

每日一练社区's avatar
每日一练社区 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
```cpp
class Solution
{
public:
    vector<TreeNode *> m[9][9];

    vector<TreeNode *> generate(int left, int right)
    {
        if (left > right)
            return {nullptr};
        if (!m[left][right].empty())
            return m[left][right];

        vector<TreeNode *> trees;
        for (int i = left; i <= right; i++)
        {
            vector<TreeNode *> left_all = generate(left, i - 1);
            vector<TreeNode *> right_all = generate(i + 1, right);

            for (TreeNode *l : left_all)
            {
                for (TreeNode *r : right_all)
                {
                    TreeNode *root = new TreeNode(i);
                    root->left = l;
                    root->right = r;
                    trees.emplace_back(root);
                }
            }
        }
        m[left][right] = trees;
        return trees;
    }

    vector<TreeNode *> generateTrees(int n)
    {
        return generate(1, n);
    }
};
```

### C
F
fix bug  
feilong 已提交
198

每日一练社区's avatar
每日一练社区 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
```cpp
class Solution
{
public:
    vector<TreeNode *> generateTrees(int n)
    {
        vector<TreeNode *> res;
        if (n == 0)
        {
            return res;
        }
        return gem(1, n);
    }
    vector<TreeNode *> gem(int start, int end)
    {
        vector<TreeNode *> res;
        if (start > end)
        {
            res.push_back(NULL);
        }
        for (int i = start; i <= end; i++)
        {
            vector<TreeNode *> lefts = gem(start, i - 1);
            vector<TreeNode *> rights = gem(i + 1, end);
            for (auto left : lefts)
            {
                for (auto right : rights)
                {
                    TreeNode *temp = new TreeNode(i);
                    temp->left = left;
                    temp->right = right;
                    res.push_back(temp);
                }
            }
        }
        return res;
    }
};
```