TWO OF THE FOUR TEST CASES ARE GIVING WRONG ANSWER.
import java.util.*;
public class TreeLeftView
{
Scanner s = new Scanner(System.in);
private class Node
{
int data;
int hd;
Node left;
Node right;
public Node(int data)
{
this.data = data;
}
}
private Node root;
public TreeLeftView()
{
root = takeInput();
}
public Node takeInput()
{
int rd = s.nextInt();
if (rd == -1)
return null;
Node rn = new Node(rd);
Queue<Node> q = new LinkedList<>();
q.add(rn);
while (!q.isEmpty())
{
Node pn = q.remove();
int lcd = s.nextInt();
if (lcd != -1)
{
Node lcn = new Node(lcd);
pn.left = lcn;
q.add(lcn);
}
int rcd = s.nextInt();
if (rcd != -1)
{
Node rcn = new Node(rcd);
pn.right = rcn;
q.add(rcn);
}
}
return rn;
}
public void leftView(Node node)
{
if(node==null)
return;
System.out.print(node.data+" ");
if(node.left!=null)
leftView(node.left);
else if(node.right!=null)
leftView(node.right);
}
public static void main(String args[]) throws Exception
{
TreeLeftView tree = new TreeLeftView();
tree.leftView(tree.root);
}
}