-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_binary_search_trees_ii.cpp
212 lines (190 loc) · 5.57 KB
/
unique_binary_search_trees_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
155
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// https://leetcode.com/problems/unique-binary-search-trees-ii/
// July 10, 2016
#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*> generateTrees(int n)
{
vector<TreeNode*> vecTree;
if (n < 1)
{
return vecTree;
}
vector<int> vecNum;
for (int i = 1; i != (n+1); ++i)
{
vecNum.push_back(i);
}
vecTree = generateTrees(vecNum, 0, vecNum.size()-1);
return vecTree;
}
void print_vector_subtree_top_two_level(vector<TreeNode*> vecTree)
{
for (vector<TreeNode*>::iterator it = vecTree.begin(); it != vecTree.end(); ++it)
{
if (*it == NULL)
{
cout << "\t\troot: " << "null" << endl;
continue;
}
cout << "\t\troot: " << (*it)->val;
cout << " :: left: ";
if ((*it)->left)
{
cout << (*it)->left->val;
}
else
{
cout << "null";
}
cout << " :: right: ";
if ((*it)->right)
{
cout << (*it)->right->val;
}
else
{
cout << "null";
}
cout << endl;
}
}
void print_subtree_top_two_level(TreeNode* root)
{
if (root == NULL)
{
cout << "\t\troot: " << "null" << endl;
return;
}
cout << "\t\troot: " << root->val;
cout << " :: left: ";
if (root->left)
{
cout << root->left->val;
}
else
{
cout << "null";
}
cout << " :: right: ";
if (root->right)
{
cout << root->right->val;
}
else
{
cout << "null";
}
cout << endl;
}
private:
// inclusive of st_index, en_index
vector<TreeNode*> generateTrees(vector<int>& nums, int st_index, int en_index)
{
vector<TreeNode*> vecTree;
// cout << "st_index: " << st_index << " :: en_index: " << en_index << endl;
for (int i = st_index; i != (en_index+1); ++i)
{
// cout << "\troot index: " << i << " :: st_index: " << st_index << " :: en_index: " << en_index << endl;
// TreeNode* root_sub_tree = new TreeNode(nums[i]);
vector<TreeNode*> vecLeftSubTree;
if (st_index < i)
{
vecLeftSubTree = generateTrees(nums, st_index, i-1);
}
else
{
vecLeftSubTree.push_back(NULL);
}
vector<TreeNode*> vecRightSubtree;
if (i < en_index)
{
vecRightSubtree = generateTrees(nums, i+1, en_index);
}
else
{
vecRightSubtree.push_back(NULL);
}
/*
if (i == 0)
{
cout << "\t\tprint right subtree when root index=0" << endl;
print_vector_subtree_top_two_level(vecRightSubtree);
}
*/
/*
if ((st_index == 0) && (en_index == 2))
{
print_vector_subtree_top_two_level(vecRightSubtree);
}
*/
// Now create the subtrees with root i
for (vector<TreeNode*>::iterator it_left = vecLeftSubTree.begin(); it_left != vecLeftSubTree.end(); ++it_left)
{
for (vector<TreeNode*>::iterator it_right = vecRightSubtree.begin(); it_right != vecRightSubtree.end(); ++it_right)
{
TreeNode* root_sub_tree = new TreeNode(nums[i]);
root_sub_tree->left = *it_left;
root_sub_tree->right = *it_right;
/*
cout << "root: " << root_sub_tree->val << " :: left: ";
if (root_sub_tree->left)
{
cout << root_sub_tree->left->val;
}
else
{
cout << "null";
}
cout << " :: right: ";
if (root_sub_tree->right)
{
cout << root_sub_tree->right->val;
}
else
{
cout << "null";
}
cout << endl;
*/
/*
if ((st_index == 0) && (en_index == 2))
{
// print_subtree_top_two_level(root_sub_tree);
}
*/
vecTree.push_back(root_sub_tree);
}
}
/*
if ((st_index == 0) && (en_index == 2))
{
print_vector_subtree_top_two_level(vecTree);
}
*/
}
return vecTree;
}
};
int main(int argc, char** argv)
{
int n = 4;
Solution sln;
vector<TreeNode*> vecTree = sln.generateTrees(n);
cout << "number of trees: " << vecTree.size() << endl;
sln.print_vector_subtree_top_two_level(vecTree);
return 0;
}
/**
* Had faced issues as left/right pointer got changed when creating the next tree for vecTree
* root_sub_tree: This needs to be created every time.
*/