Print nodes between two given level numbers of a binary tree | c # 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
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void printLevels ( Node node , int low , int high ) { LinkedList < Node > Q = new LinkedList < Node > ( ) ; Node marker = new Node ( 4 ) ; int level = 1 ; Q . AddLast ( node ) ; Q . AddLast ( marker ) ; while ( Q . Count > 0 ) { Node n = Q . First . Value ; Q . RemoveFirst ( ) ; if ( n == marker ) { Console . WriteLine ( " " ) ; level ++ ; if ( Q . Count == 0 level > high ) { break ; } Q . AddLast ( marker ) ; continue ; } if ( level >= low ) { Console . Write ( n . data + " ▁ " ) ; } if ( n . left != null ) { Q . AddLast ( n . left ) ; } if ( n . right != null ) { Q . AddLast ( 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 ) ; Console . Write ( " Level ▁ Order ▁ traversal ▁ between ▁ given ▁ two ▁ levels ▁ is ▁ " ) ; tree . printLevels ( tree . root , 2 , 3 ) ; } }