using
System;
using
System.Collections.Generic;
public
class
Node
{
public
int
data;
public
Node left, right;
public
Node(
int
data)
{
this
.data = data;
left = right =
null
;
}
}
public
class
GFG
{
static
void
LevelOrderTraversal(Node root)
{
if
(root ==
null
)
return
;
Queue<Node> q =
new
Queue<Node>();
q.Enqueue(root);
int
lvl = 1;
while
(q.Count != 0)
{
int
n = q.Count;
for
(
int
i = 0; i < n; i++)
{
Node node = q.Peek();
double
num = Math.Sqrt(node.data);
int
x1 = (
int
)Math.Floor(num);
int
x2 = (
int
)Math.Ceiling(num);
if
(x1 == x2)
{
if
(((lvl & 1) != 0) && !((x1 & 1) != 0))
{
int
num1 = x1 - 1, num2 = x1 + 1;
node.data = (Math.Abs(node.data - num1 * num1) <
Math.Abs(node.data - num2 * num2)) ?
(num1 * num1) : (num2 * num2);
}
if
(!((lvl & 1) != 0) && ((x1 & 1) != 0))
{
int
num1 = x1 - 1, num2 = x1 + 1;
node.data = (Math.Abs(node.data - num1 * num1) <
Math.Abs(node.data - num2 * num2)) ?
(num1 * num1) : (num2 * num2);
}
}
else
{
if
((lvl & 1) != 0)
node.data = (x1 & 1) != 0 ?
(x1 * x1) : (x2 * x2);
else
node.data = (x1 & 1) != 0 ?
(x2 * x2) : (x1 * x1);
}
Console.Write(node.data +
" "
);
q.Dequeue();
if
(node.left !=
null
)
q.Enqueue(node.left);
if
(node.right !=
null
)
q.Enqueue(node.right);
}
lvl++;
Console.WriteLine();
}
}
static
public
void
Main ()
{
Node root =
new
Node(5);
root.left =
new
Node(3);
root.right =
new
Node(2);
root.right.left =
new
Node(16);
root.right.right =
new
Node(19);
LevelOrderTraversal(root);
}
}