forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
95.cpp
84 lines (71 loc) · 2.29 KB
/
95.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
82
83
84
#include <iostream>
#include <vector>
#include <queue>
#include <string>
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 *> generateTrees(int n) {
vector<vector<TreeNode *> > d(n + 1);
d[0].push_back(NULL); // d[0][0]
if (n == 0) return d[0];
d[1].push_back(new TreeNode(1)); // d[1][0]
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
for (int ii = 0; ii < d[j - 1].size(); ii++) {
for (int jj = 0; jj < d[i - j].size(); jj++){
TreeNode *root = new TreeNode(j);
/* left child tree can be found in memorization */
root->left = d[j - 1][ii];
/* nodes of right child tree is bigger than root, but we
can add root's value to all nodes in memorization to
build the correct tree */
root->right = addNodes(d[i - j][jj], j);
d[i].push_back(root); // d[i][ii * jj]
}
}
}
}
return d[n];
}
TreeNode * addNodes(TreeNode *root, int x) {
if (!root) return NULL;
TreeNode *p = new TreeNode(root->val + x);
p->left = addNodes(root->left, x);
p->right = addNodes(root->right, x);
return p;
}
void printTreeLevelOrder(TreeNode *root) {
string buf;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
TreeNode *p = q.front();
q.pop();
if (p) {
buf.push_back(p->val + '0');
if (p->left || p->right){
q.push(p->left);
q.push(p->right);
}
}
else
buf.push_back('#');
}
cout << buf << endl;
}
};
int main() {
Solution s;
vector<TreeNode *> ret = s.generateTrees(4);
for (int i = 0; i < ret.size(); i++) {
s.printTreeLevelOrder(ret[i]);
}
return 0;
}