-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path不同的二叉搜索树 II.cpp
84 lines (66 loc) · 1.46 KB
/
不同的二叉搜索树 II.cpp
1
2
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <functional>
using namespace std;
/*
这题很明显是用递归来做,但我就是不会写。
以第k个节点为根的所有二叉树,其左子树是[1,k-1]的所有二叉树组成,右子树是[k+1,n]的所有二叉树组成。
所以需要建立两个vector<TreeNode *>的表来存储左子树和右子树。
然后将这两个表两两组合。
https://blog.csdn.net/magicbean2/article/details/70254731
*/
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)
{
vector<TreeNode *> res;
if (n < 1)
return res;
dfs(1, n, res);
return res;
}
void dfs(int start, int end, vector<TreeNode *> &tree)
{
if (start > end)
{
tree.push_back(nullptr);
return;
}
for (int i = start; i <= end; ++i)
{
vector<TreeNode *> left;
vector<TreeNode *> right;
dfs(start, i - 1, left);
dfs(i + 1, end, right);
for (int m = 0; m < left.size(); ++m)
{
for (int n = 0; n < right.size(); ++n)
{
TreeNode *root = new TreeNode(i);
root->left = left[m];
root->right = right[n];
tree.push_back(root);
}
}
}
}
};
int main()
{
Solution a;
vector<TreeNode*> b = a.generateTrees(3);
system("pause");
return 0;
}