Perfect Binary Tree Specific Level Order Traversal | Set 2 | C # program for special order traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Function body ; Create a queue and enqueue left and right children of root ; We process two nodes at a time , so we need two variables to store two front items of queue ; traversal loop ; Pop two items from queue ; Push first and second node 's  children in reverse order ; If first and second have grandchildren , enqueue them in specific order ; Given a perfect binary tree , print its nodes in specific level order ; create a stack and push root ; Push level 1 and level 2 nodes in stack ; Since it is perfect Binary Tree , right is not checked ; Do anything more if there are nodes at next level in given perfect Binary Tree ; Finally pop all Nodes from stack and prints them . ; Driver Code
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class GFG { public Node root ; public virtual void printSpecificLevelOrderUtil ( Node root , Stack < Node > s ) { if ( root == null ) { return ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( root . left ) ; q . AddLast ( root . right ) ; Node first = null , second = null ; while ( q . Count > 0 ) { first = q . First . Value ; q . RemoveFirst ( ) ; second = q . First . Value ; q . RemoveFirst ( ) ; s . Push ( second . left ) ; s . Push ( first . right ) ; s . Push ( second . right ) ; s . Push ( first . left ) ; if ( first . left . left != null ) { q . AddLast ( first . right ) ; q . AddLast ( second . left ) ; q . AddLast ( first . left ) ; q . AddLast ( second . right ) ; } } } public virtual void printSpecificLevelOrder ( Node root ) { Stack < Node > s = new Stack < Node > ( ) ; s . Push ( root ) ; if ( root . left != null ) { s . Push ( root . right ) ; s . Push ( root . left ) ; } if ( root . left . left != null ) { printSpecificLevelOrderUtil ( root , s ) ; } while ( s . Count > 0 ) { Console . Write ( s . Peek ( ) . data + " ▁ " ) ; s . Pop ( ) ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; Console . WriteLine ( " Specific ▁ Level ▁ Order ▁ Traversal ▁ " + " of ▁ Binary ▁ Tree ▁ is ▁ " ) ; tree . printSpecificLevelOrder ( tree . root ) ; }