import java.util.*;
class Node
{
int key;
List<Node> child;
public Node(int key)
{
this.key = key;
child = new ArrayList<Node>();
}
}
class Main
{
static void mirrorTree(Node root)
{
if (root == null)
return;
int n = root.child.size();
if (n < 2)
return;
for (int i = 0; i < n; i++)
mirrorTree(root.child.get(i));
Collections.reverse(root.child);
}
public static Node newNode(int key)
{
Node temp = new Node(key);
return temp;
}
static void printNodeLevelWise(Node root)
{
if (root == null)
return;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty())
{
int n = q.size();
while (n > 0)
{
Node p = q.poll();
System.out.print(p.key + " ");
for (int i = 0; i < p.child.size(); i++)
q.add(p.child.get(i));
n--;
}
System.out.println();
}
}
public static void main(String[] args)
{
Node root = newNode(10);
root.child.add(newNode(2));
root.child.add(newNode(34));
root.child.add(newNode(56));
root.child.add(newNode(100));
root.child.get(2).child.add(newNode(1));
root.child.get(3).child.add(newNode(7));
root.child.get(3).child.add(newNode(8));
root.child.get(3).child.add(newNode(9));
System.out.println("Level order traversal Before Mirroring");
printNodeLevelWise(root);
mirrorTree(root);
System.out.println("\nLevel order traversal After Mirroring");
printNodeLevelWise(root);
}
}