-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dfs_n3.cpp
57 lines (46 loc) · 1.15 KB
/
AC_dfs_n3.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_n3.cpp
* Create Date: 2015-03-08 20:55:07
* Descripton: Just like the I version said.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
// Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
private:
vector<TreeNode *> generate(int op, int ed) {
vector<TreeNode *> ret;
if (op > ed) {
ret.push_back(NULL);
return ret;
}
vector<TreeNode *> left, right;
for (int k = op; k <= ed; ++k) {
left = generate(op, k - 1);
right = generate(k + 1, ed);
for (auto &i : left)
for (auto &j : right) {
TreeNode *root = new TreeNode(k);
root->left = i;
root->right = j;
ret.push_back(root);
}
}
return ret;
}
public:
vector<TreeNode *> generateTrees(int n) {
return generate(1, n);
}
};
int main() {
return 0;
}