import java.util.LinkedList;
class Node {
public Node left;
public int data;
public Node right;
public Node(int val) {
this.data = val;
}
@Override
return this.data + "";
}
}
class LeftView {
public void leftViewQueue(Node root) {
boolean isFirstNode = false;
LinkedList<Node> queue = new LinkedList<Node>();
queue.addLast(root); //enqueue
queue.addLast(null); //enqueue
System.
out.
print(root.
data + "\t"); while (!queue.isEmpty()) {
Node poped = queue.removeFirst(); //dequeue
if (poped == null) {
isFirstNode = true; //current level processed , push null for next level
queue.addLast(null);
}
else {
if(isFirstNode == true)
{
System.
out.
print(poped.
data + "\t"); // Print first Node isFirstNode = false; //and set flag to be false until next null encountered
}
if (poped.left != null)
queue.addLast(poped.left);
if (poped.right != null)
queue.addLast(poped.right);
}
}
}
public static void main
(String[] args
) { Node root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.left.right.left = new Node(7);
root.left.right.right = new Node(8);
root.left.right.left.left = new Node(9);
root.right = new Node(3);
root.right.left = new Node(6);
root.right.right = new Node(7);
LeftView obj = new LeftView();
obj.leftViewQueue(root);
}
}