Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
#embody <bits/stdc++.h>
utilizing
namespace
std;
struct
Node {
char
val;
vector<Node*> kids;
};
Node* newNode(
int
key)
{
Node* temp =
new
Node;
temp->val = key;
return
temp;
}
vector<vector<
int
> > levelOrder(Node* root)
{
vector<vector<
int
> > ans;
if
(!root)
cout <<
"N-Ary tree doesn't any nodes"
;
queue<Node*> main_queue;
main_queue.push(root);
vector<
int
> temp;
whereas
(!main_queue.empty()) {
int
n = main_queue.dimension();
for
(
int
i = 0; i < n; i++) {
Node* cur = main_queue.entrance();
main_queue.pop();
temp.push_back(cur->val);
for
(
auto
u : cur->kids)
main_queue.push(u);
}
ans.push_back(temp);
temp.clear();
}
return
ans;
}
int
foremost()
{
Node* root = newNode(1);
root->kids.push_back(newNode(3));
root->kids.push_back(newNode(2));
root->kids.push_back(newNode(4));
root->kids[0]->kids.push_back(newNode(5));
root->kids[0]->kids.push_back(newNode(6));
vector<vector<
int
> > ans = levelOrder(root);
for
(
auto
v : ans) {
for
(
int
x : v)
cout << x <<
" "
;
cout << endl;
}
return
0;
}
[ad_2]