import java.util.*;
public class Main
{
static class Node {
public int val;
public Vector<Node> child;
public Node(int key)
{
val = key;
child = new Vector<Node>();
}
}
static Node newNode(int key)
{
Node temp = new Node(key);
return temp;
}
static void getLeftRightview(Node root)
{
Vector<Node> curNodes = new Vector<Node>();
curNodes.add(root);
Vector<Integer> leftView = new Vector<Integer>();
Vector<Integer> rightView = new Vector<Integer>();
while (curNodes.size() > 0) {
int n = curNodes.size();
for (int i = 0; i < n; i++) {
Node cur = (Node)curNodes.get(0);
curNodes.remove(0);
if (i == 0)
leftView.add(cur.val);
if (i == n - 1)
rightView.add(cur.val);
for (int it = 0; it < cur.child.size(); it++) {
curNodes.add(cur.child.get(it));
}
}
}
System.out.print("Left View: ");
for (int i = 0; i < leftView.size(); i++)
System.out.print(leftView.get(i) + " ");
System.out.println();
System.out.print("RIght View: ");
for (int i = 0; i < rightView.size(); i++)
System.out.print(rightView.get(i) + " ");
}
public static void main(String[] args) {
Node root = newNode(1);
(root.child).add(newNode(2));
(root.child).add(newNode(3));
(root.child.get(0).child).add(newNode(4));
(root.child.get(1).child).add(newNode(6));
(root.child.get(0).child).add(newNode(5));
(root.child.get(1)).child.add(newNode(7));
(root.child.get(1).child).add(newNode(8));
getLeftRightview(root);
}
}