-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_trees.cpp
More file actions
145 lines (110 loc) · 3.37 KB
/
unique_trees.cpp
File metadata and controls
145 lines (110 loc) · 3.37 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
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
/**
Adam Rosenberg
Input is a variety of binary search trees, and the output is the number of unique shapes of trees.
**/
#include <vector>
#include <sstream>
#include <iostream>
#include <set>
using namespace std;
class node{
public:
int data;
node* leftchild;
node* rightchild;
int hasbeenchecked;
public:
node(int data){
this->data = data;
leftchild = 0;
rightchild = 0;
hasbeenchecked=0;
}
};
node* insert(int data, node* root){
if(root == NULL){
root = new node(data);
root->data = data;
return root;
}
else{
node* current = root;
do{
if (data > current->data) {
if (current->rightchild == NULL) {
current->rightchild = new node(data);
current->rightchild->data = data;
// current->rightchild->rightchild = 0;
// current->rightchild->leftchild = 0;
current = NULL;
} else {
current = current->rightchild;
}
} else if (data < current->data) {
if (current->leftchild == NULL) {
current->leftchild = new node(data);
current->leftchild->data = data;
// current->leftchild->rightchild = 0;
// current->leftchild->leftchild = 0;
current = NULL;
} else {
current = current->leftchild;
}
}
}while(current!=NULL);
return root;
}
}
int uniqueshape(struct node* first, struct node* second){
if(first == NULL || second == NULL)
return((first == NULL) & (second==NULL));
return (uniqueshape(first->rightchild, second->rightchild) && uniqueshape(first->leftchild, second->leftchild));
}
int main(int argc, char* argv[]){
vector<node*> forest; //group of trees
int currentlevel = 0;
int height;
string first;
getline(cin,first);
stringstream ss(first);
if(ss.good())
ss >> first;
height = stoi(first);
for(int i = 0; i < height; i++){
node* rootptr = 0;
string indexline;
int val = 0;
getline(cin,indexline);
stringstream ss(indexline);
while(ss >> val){
rootptr = insert(val, rootptr);
}
forest.push_back(rootptr);
}
int equaltrees = 0;
for(int outer = 0; outer < forest.size()-1; outer++){
for(int inner = forest.size()-1; inner > outer; inner--){
if(!forest.at(inner)->hasbeenchecked){
if(uniqueshape(forest.at(outer), forest.at(inner))){
forest.at(inner)->hasbeenchecked = 1;
// cout << "tree at " << outer << " equals tree at " << inner << endl;
equaltrees++;
}
}
}
}
for(const auto& i: forest){
}
int sum=0;
for(int i = 0; i < forest.size(); i++){
sum += i;
}
// cout << "equal trees, sum, forest.size() " << equaltrees << " " << sum << " " << forest.size() << endl;
if(equaltrees == sum){
cout<<"1" << endl;
}
else{
cout << forest.size() - equaltrees << endl;
}
return 0;
}