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 ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a perfect binary tree , print its nodes in specific level order ; for level order traversal ; Stack to print reverse ; vector to store the level ; considering size of the level ; push data of the node of a particular level to vector ; push vector containing a level in Stack ; print the Stack ; Finally pop all Nodes from Stack and prints them . ; finally print root ; ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left ; public Node right ; public Node ( int value ) { data = value ; left = null ; right = null ; } } ; static void specific_level_order_traversal ( Node root ) { Queue < Node > q = new Queue < Node > ( ) ; Stack < List < int > > s = new Stack < List < int > > ( ) ; q . Enqueue ( root ) ; int sz ; while ( q . Count > 0 ) { List < int > v = new List < int > ( ) ; sz = q . Count ; for ( int i = 0 ; i < sz ; ++ i ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; v . Add ( temp . data ) ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; } s . Push ( v ) ; } while ( s . Count > 0 ) { List < int > v = s . Peek ( ) ; s . Pop ( ) ; for ( int i = 0 , j = v . Count - 1 ; i < j ; ++ i ) { Console . Write ( v [ i ] + " ▁ " + v [ j ] + " ▁ " ) ; j -- ; } } Console . WriteLine ( root . data ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; Console . WriteLine ( " Specific ▁ Level ▁ Order ▁ traversal " + " ▁ of ▁ binary ▁ tree ▁ is " ) ; specific_level_order_traversal ( root ) ; } }