-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstruct-distinct-BST-1-to-n.cpp
74 lines (65 loc) · 1.43 KB
/
construct-distinct-BST-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @file construct-distinct-BST-1-to-n.cpp
* @author prakash (prakashsellathurai@gmail.com)
* @brief
* @version 0.1
* @date 2021-08-03
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
#include <vector>
using namespace std;
typedef struct Node {
int data;
Node *left;
Node *right;
} Node;
struct Node *newNode(int data) {
Node *node = new Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void preorder(Node *root) {
if (root == NULL) {
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
vector<Node *> constructTrees(int start, int end) {
vector<Node *> result;
if (start > end) {
result.push_back(NULL);
return result;
}
for (int i = start; i <= end; i++) {
vector<Node *> left = constructTrees(start, i - 1);
vector<Node *> right = constructTrees(i + 1, end);
for (int j = 0; j < left.size(); j++) {
for (int k = 0; k < right.size(); k++) {
Node *root = newNode(i);
root->left = left[j];
root->right = right[k];
result.push_back(root);
}
}
}
return result;
}
void construct_distinct_BST(int n) {
vector<Node *> resullts = constructTrees(1, n);
for (int i = 0; i < resullts.size(); i++) {
cout << "BST " << i << ": ";
preorder(resullts[i]);
cout << endl;
}
}
int main(int argc, const char **argv) {
construct_distinct_BST(3);
return 0;
}