forked from itsyadavRajkumar/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtRee.cpp
More file actions
59 lines (53 loc) · 1.07 KB
/
tRee.cpp
File metadata and controls
59 lines (53 loc) · 1.07 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
#include <bits/stdc++.h>
#define N 1e6
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
class Tree {
public:
int data;
vector<Tree *> children;
Tree(int data) {
this->data = data;
}
};
Tree *createTree() {
int rootData; cin >> rootData;
Tree* root = new Tree(rootData);
queue<Tree* > pendingRoot;
pendingRoot.push(root);
while (!pendingRoot.empty()) {
int numChildren; cin >> numChildren;
Tree* front = pendingRoot.front();
pendingRoot.pop();
for (int i = 0; i < numChildren; ++i) {
int childrenData; cin >> childrenData;
Tree* child = new Tree(childrenData);
front->children.pb(child);
pendingRoot.push(child);
}
}
return root;
}
void printTree(Tree* root) {
if (root == NULL) return;
cout << root->data << " : ";
for (int i = 0; i < root -> children.size(); ++i) {
cout << root->children[i]->data << ' ';
}
cout << '\n';
for (int i = 0; i < root->children.size() ; ++i) {
printTree(root->children[i]);
}
}
// 1
// 3
// 2 3 4
// 1 5
// 1 6
// 1 7
// 0 0 0
int main() {
fast;
Tree* root = createTree();
printTree(root);
return 0;
}