import
java.util.*;
class
Node {
int
data;
ArrayList<Node> child;
Node(
int
data)
{
this
.data = data;
this
.child =
new
ArrayList<>();
}
}
class
Main {
static
List<List<Integer> > adj =
new
ArrayList<>();
static
void
DFS(Node node)
{
for
(Node x : node.child) {
if
(x !=
null
) {
adj.get(node.data).add(x.data);
adj.get(x.data).add(node.data);
DFS(x);
}
}
}
static
void
levelOrderTrav(
int
s,
int
N)
{
Queue<Integer> q =
new
LinkedList<>();
boolean
[] visited =
new
boolean
[N +
1
];
q.offer(s);
q.offer(-
1
);
visited[s] =
true
;
while
(!q.isEmpty()) {
int
v = q.poll();
if
(v == -
1
) {
if
(!q.isEmpty())
q.offer(-
1
);
System.out.println();
continue
;
}
System.out.print(v +
" "
);
for
(
int
u : adj.get(v)) {
if
(!visited[u]) {
visited[u] =
true
;
q.offer(u);
}
}
}
}
public
static
void
main(String[] args)
{
Node root =
new
Node(
1
);
root.child.add(
new
Node(
2
));
root.child.add(
new
Node(
3
));
root.child.add(
new
Node(
4
));
root.child.add(
new
Node(
5
));
root.child.get(
0
).child.add(
new
Node(
6
));
root.child.get(
0
).child.add(
new
Node(
7
));
root.child.get(
2
).child.add(
new
Node(
8
));
root.child.get(
3
).child.add(
new
Node(
9
));
root.child.get(
3
).child.add(
new
Node(
10
));
root.child.get(
3
).child.add(
new
Node(
11
));
int
N =
11
;
int
K =
5
;
for
(
int
i =
0
; i <= N; i++)
adj.add(
new
ArrayList<>());
DFS(root);
levelOrderTrav(K, N);
}
}