Print nodes between two given level numbers of a binary tree | Java program to print Nodes level by level between given two levels ; A binary tree Node has key , pointer to left and right children ; Given a binary tree , print nodes from level number ' low ' to level number ' high ' ; Marker node to indicate end of level ; Initialize level number ; Enqueue the only first level node and marker node for end of level ; Simple level order traversal loop ; Remove the front item from queue ; Check if end of level is reached ; print a new line and increment level number ; Check if marker node was last node in queue or level number is beyond the given upper limit ; Enqueue the marker for end of next level ; If this is marker , then we don 't need print it  and enqueue its children ; If level is equal to or greater than given lower level , print it ; Enqueue children of non - marker node ; Driver program to test for above functions
import java . util . LinkedList ; import java . util . Queue ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printLevels ( Node node , int low , int high ) { Queue < Node > Q = new LinkedList < > ( ) ; Node marker = new Node ( 4 ) ; int level = 1 ; Q . add ( node ) ; Q . add ( marker ) ; while ( Q . isEmpty ( ) == false ) { Node n = Q . peek ( ) ; Q . remove ( ) ; if ( n == marker ) { System . out . println ( " " ) ; level ++ ; if ( Q . isEmpty ( ) == true level > high ) break ; Q . add ( marker ) ; continue ; } if ( level >= low ) System . out . print ( n . data + " ▁ " ) ; if ( n . left != null ) Q . add ( n . left ) ; if ( n . right != null ) Q . add ( n . right ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; System . out . print ( " Level ▁ Order ▁ traversal ▁ between ▁ given ▁ two ▁ levels ▁ is ▁ " ) ; tree . printLevels ( tree . root , 2 , 3 ) ; } }