import java.util.*;
public class Main {
public static class Node{
int data;
Node left , right;
Node (int d){
data = d;
right = left = null;
}
}
static Node root;
static Queue q = new LinkedList();
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) constructBST(sc.nextInt());
leftView();
}
public static void constructBST(int data){
Node n = new Node(data);
if(root == null) root = n;
if(q.peek().left == null){
q.peek().left = n;
}
else{
q.peek().right = n;
q.remove();
}
q.add(n);
}
public static void leftView(){
leftViewCode(root , 1);
}
static int m = 0;
public static void leftViewCode(Node node , int l){
if(node == null) return;
if(m < l) {
System.out.print(" " + node.data);
m = l;
}
leftViewCode(node.left , l+1);
leftViewCode(node.right , l+1);
}
}
Please help me :( not getting
see this:
1 Like