using
System;
using
System.Collections.Generic;
class
GFG
{
class
pair
{
public
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
int
sz = (
int
) 1e5;
static
int
maxLevel = 0;
static
List<
int
> []tree =
new
List<
int
>[sz + 1];
static
bool
[]vis =
new
bool
[sz + 1];
static
int
[]level =
new
int
[sz + 1];
static
List<
int
> []nodes =
new
List<
int
>[sz + 1];
static
void
addEdge(
int
a,
int
b)
{
tree[a].Add(b);
tree[b].Add(a);
}
static
void
bfs(
int
node)
{
Queue<pair> qu =
new
Queue<pair>();
qu.Enqueue(
new
pair( node, 0 ));
nodes[0].Add(node);
vis[node] =
true
;
level[1] = 0;
while
(qu.Count != 0) {
pair p = qu.Peek();
qu.Dequeue();
vis[p.first] =
true
;
foreach
(
int
child
in
tree[p.first]) {
if
(!vis[child]) {
qu.Enqueue(
new
pair( child, p.first ));
level[child] = level[p.first] + 1;
maxLevel = Math.Max(maxLevel, level[child]);
nodes[level[child]].Add(child);
}
}
}
}
static
void
display()
{
int
k = 0, path = 0;
int
condn = (maxLevel) / 2 + 1;
bool
flag =
true
;
while
(condn-- > 0) {
if
(flag) {
int
j = nodes[k].Count - 1;
for
(j = 0; j < nodes[k].Count - path; j++)
Console.Write(nodes[k][j]+
" "
);
k++;
while
(k < maxLevel) {
j = nodes[k].Count - 1;
Console.Write(nodes[k][j - path]+
" "
);
k++;
}
j = nodes[k].Count - 1;
if
(k > path)
for
(j -= path; j >= 0; j--)
Console.Write(nodes[k][j]+
" "
);
maxLevel--;
k--;
path++;
flag = !flag;
}
else
{
int
j = nodes[k].Count - 1;
for
(j = 0; j < nodes[k].Count - path; j++)
Console.Write(nodes[k][j]+
" "
);
maxLevel--;
k--;
while
(k > path) {
int
c = nodes[k].Count - 1;
Console.Write(nodes[k]+
" "
);
k--;
}
j = nodes[k].Count - 1;
if
(k == path)
for
(j -= path; j >= 0; j--)
Console.Write(nodes[k][j]+
" "
);
path++;
k++;
flag = !flag;
}
}
}
public
static
void
Main(String[] args)
{
for
(
int
i = 0; i < tree.Length; i++) {
tree[i] =
new
List<
int
>();
nodes[i] =
new
List<
int
>();
}
for
(
int
i = 1; i <= 5; i++) {
addEdge(i, 2 * i);
addEdge(i, 2 * i + 1);
}
bfs(1);
display();
}
}