Print Left View of a Binary Tree | Java program to print left view of Binary Tree ; Binary tree node ; function to print left view of binary tree ; number of nodes at current level ; Traverse all nodes of current level ; Print the left most element at the level ; Add left node to queue ; Add right node to queue ; Driver code ; construct binary tree as shown in above diagram
import java . util . * ; public class PrintRightView { private static class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } private static void printLeftView ( Node root ) { if ( root == null ) return ; Queue < Node > queue = new LinkedList < > ( ) ; queue . add ( root ) ; while ( ! queue . isEmpty ( ) ) { int n = queue . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { Node temp = queue . poll ( ) ; if ( i == 1 ) System . out . print ( temp . data + " ▁ " ) ; if ( temp . left != null ) queue . add ( temp . left ) ; if ( temp . right != null ) queue . add ( temp . right ) ; } } } public static void main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 8 ) ; root . right . right = new Node ( 15 ) ; root . right . left = new Node ( 12 ) ; root . right . right . left = new Node ( 14 ) ; printLeftView ( root ) ; } }