-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreePrint.cpp
More file actions
34 lines (30 loc) · 770 Bytes
/
TreePrint.cpp
File metadata and controls
34 lines (30 loc) · 770 Bytes
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
#include <iostream>
#include <queue>
#include "../TreeNode.h"
using namespace std;
void printTreeLevelWise(TreeNode<int>* root) {
if (root == nullptr) {
return;
}
queue<TreeNode<int>*> pending;
pending.push(root);
while (!pending.empty()) {
auto front = pending.front();
pending.pop();
cout << front->data << ":";
for (int i = 0; i < front->children.size(); i++) {
auto child = front->children[i];
cout << child->data;
if (i != front->children.size() - 1) {
cout << ",";
}
pending.push(child);
}
cout << endl;
}
}
int main() {
auto root = takeInputLevelWise();
printTreeLevelWise(root);
return 0;
}