Print nodes between two given level numbers of a binary tree | 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
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } var root = null ; function printLevels ( node , low , high ) { var Q = [ ] ; var marker = new Node ( 4 ) ; var level = 1 ; Q . push ( node ) ; Q . push ( marker ) ; while ( Q . length > 0 ) { var n = Q [ 0 ] ; Q . shift ( ) ; if ( n == marker ) { document . write ( " " ) ; level ++ ; if ( Q . length == 0 level > high ) { break ; } Q . push ( marker ) ; continue ; } if ( level >= low ) { document . write ( n . data + " " ) ; } if ( n . left != null ) { Q . push ( n . left ) ; } if ( n . right != null ) { Q . push ( n . right ) ; } } } root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 12 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; document . write ( " " ) ; printLevels ( root , 2 , 3 ) ;