-
Notifications
You must be signed in to change notification settings - Fork 700
Expand file tree
/
Copy pathn_Possible_BST.cpp
More file actions
52 lines (47 loc) · 1.11 KB
/
n_Possible_BST.cpp
File metadata and controls
52 lines (47 loc) · 1.11 KB
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
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *left,*right;
node(int d){
data=d;
left=NULL;
right=NULL;
}
};
vector<node*> possible_bst(int start,int end){
vector<node*> trees;
if(start>end){
trees.push_back(NULL);
return trees;
}
for(int i=start;i<=end;i++){
vector<node*> lefttree=possible_bst(start,i-1);
vector<node*> righttree=possible_bst(i+1,end);
for(int j=0;j<lefttree.size();j++){
node* left=lefttree[j];
for(int k=0;k<righttree.size();k++){
node* right=righttree[k];
node* root=new node(i);
root->left=left;
root->right=right;
trees.push_back(root);
}
}
}
return trees;
}
void display(node* root){
if(root==NULL)
return;
cout<<root->data<<" ";
display(root->left);
display(root->right);
}
int main(){
vector<node*> totaltree=possible_bst(1,3);
for(int i=0;i<totaltree.size();i++){
display(totaltree[i]);
cout<<endl;
}
}