-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAllPossibleBSTsForKeys 1 to N.cpp
51 lines (51 loc) · 1.07 KB
/
AllPossibleBSTsForKeys 1 to N.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
#include <bits/stdc++.h> 5
using namespace std;
struct node
{
int data;
node *right,*left;
};
node* newNode(int data){
node* n = new node;
n->data = data;
n->left = n->right = NULL;
return n;
}
std::vector<node*> ConstructBSTs(int start,int end){
std::vector<node*> list;
if(start > end){
list.push_back(NULL);
return list;
}
for(int i=start;i<=end;i++){
std::vector<node*> leftSubTree = ConstructBSTs(start,i-1);
std::vector<node*> rightSubTree = ConstructBSTs(i+1,end);
for(int j=0;j<leftSubTree.size();j++){
node* left = leftSubTree[j];
for(int k=0;k<rightSubTree.size();k++){
node* right = rightSubTree[k];
node* n = newNode(i);
n->right = right;
n->left = left;
list.push_back(n);
}
}
}
return list;
}
void preOrder(node* root){
if(!root) return ;
cout << root->data <<" ";
preOrder(root->left);
preOrder(root->right);
}
int main(int argc, char const *argv[])
{
std::vector<node*> v = ConstructBSTs(1,5);
cout << "Number of BSTs is " << v.size() << endl;
for(int i=0;i<v.size();i++){
preOrder(v[i]);
cout << endl;
}
return 0;
}