using
System;
using
System.Collections.Generic;
using
System.Collections;
class
GFG{
static
int
sz = 100000;
static
int
maxLevel = 0;
static
ArrayList []tree =
new
ArrayList[sz + 1];
static
bool
[]vis =
new
bool
[sz + 1];
static
int
[]level =
new
int
[sz + 1];
static
ArrayList []nodes =
new
ArrayList[sz + 1];
static
void
addEdge(
int
a,
int
b)
{
tree[a].Add(b);
tree[b].Add(a);
}
static
void
bfs(
int
node)
{
Queue qu =
new
Queue();
qu.Enqueue(
new
KeyValuePair<
int
,
int
>(node, 0));
nodes[0].Add(node);
vis[node] =
true
;
level[1] = 0;
while
(qu.Count != 0)
{
KeyValuePair<
int
,
int
> p = (KeyValuePair<
int
,
int
>)qu.Peek();
qu.Dequeue();
vis[p.Key] =
true
;
foreach
(
int
child
in
tree[p.Key])
{
if
(!vis[child])
{
qu.Enqueue(
new
KeyValuePair<
int
,
int
>(
child, p.Key));
level[child] = level[p.Key] + 1;
maxLevel = Math.Max(maxLevel, level[child]);
nodes[level[child]].Add(child);
}
}
}
}
static
void
display()
{
for
(
int
i = maxLevel; i >= 0; i--)
{
int
len = nodes[i].Count;
for
(
int
j = 0; j < len / 2; j++)
{
Console.Write((
int
)nodes[i][j] +
" "
+
(
int
)nodes[i][len - 1 - j] +
" "
);
}
if
(len % 2 == 1)
{
Console.Write((
int
)nodes[i][len / 2] +
" "
);
}
}
}
public
static
void
Main(
string
[] args)
{
for
(
int
i = 0; i < sz + 1; i++)
{
tree[i] =
new
ArrayList();
nodes[i] =
new
ArrayList();
vis[i] =
false
;
level[i] = 0;
}
addEdge(1, 2);
addEdge(1, 3);
addEdge(2, 4);
addEdge(2, 5);
addEdge(3, 6);
bfs(1);
display();
}
}