using
System;
using
System.Collections.Generic;
public
class
GFG{
class
Node {
public
int
key;
public
List<Node> child =
new
List<Node>();
};
static
Node newNode(
int
key)
{
Node temp =
new
Node();
temp.key = key;
return
temp;
}
static
void
printCousins(Node root,
int
value)
{
if
(root ==
null
)
return
;
Queue<Node> q =
new
Queue<Node>();
q.Enqueue(root);
bool
found =
false
;
int
qsize = 0;
Node tempp;
while
(q.Count!=0 && !found) {
qsize = q.Count;
while
(qsize > 0) {
tempp = q.Peek();
q.Dequeue();
if
(found ==
true
) {
for
(
int
i = 0; i < tempp.child.Count;
i++) {
if
(tempp.child[i] !=
null
)
q.Enqueue(tempp.child[i]);
}
}
for
(
int
i = 0; i < tempp.child.Count; i++)
if
(tempp.child[i] !=
null
&& tempp.child[i].key == value)
found =
true
;
if
(found ==
false
) {
for
(
int
i = 0; i < tempp.child.Count;
i++) {
if
(tempp.child[i] !=
null
)
q.Enqueue(tempp.child[i]);
}
}
qsize--;
}
}
if
(found) {
qsize = q.Count;
if
(qsize == 0)
Console.Write(
"NA"
);
for
(
int
i = 0; i < qsize; i++) {
tempp = q.Peek();
q.Dequeue();
Console.Write(tempp.key+
" "
);
}
}
else
{
Console.Write(
"Not Possible"
);
}
Console.Write(
"\n"
);
return
;
}
public
static
void
Main(String[] args)
{
Node root = newNode(10);
(root.child).Add(newNode(77));
(root.child).Add(newNode(90));
(root.child).Add(newNode(35));
(root.child).Add(newNode(19));
(root.child[0].child).Add(newNode(88));
(root.child[0].child).Add(newNode(98));
(root.child[0].child[1].child)
.Add(newNode(76));
(root.child[0].child[1].child)
.Add(newNode(20));
(root.child[1].child).Add(newNode(61));
(root.child[1].child).Add(newNode(74));
(root.child[2].child).Add(newNode(39));
(root.child[3].child).Add(newNode(17));
(root.child[3].child).Add(newNode(72));
(root.child[3].child).Add(newNode(19));
int
value = 39;
printCousins(root, value);
}
}