Perfect Binary Tree Specific Level Order Traversal | C # program for special level order traversal ; Class containing left and right child of current node and key value ; Given a perfect binary tree , print its nodes in specific level order ; Let us print root and next level first ; 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 ; 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 ; Print children of first and second in reverse order ; If first and second have grandchildren , enqueue them in reverse order ; Driver Code
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 ; } } class GFG { public Node root ; public virtual void printSpecificLevelOrder ( Node node ) { if ( node == null ) { return ; } Console . Write ( node . data ) ; if ( node . left != null ) { Console . Write ( " ▁ " + node . left . data + " ▁ " + node . right . data ) ; } if ( node . left . left == null ) { return ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( node . left ) ; q . AddLast ( node . right ) ; Node first = null , second = null ; while ( q . Count > 0 ) { first = q . First . Value ; q . RemoveFirst ( ) ; second = q . First . Value ; q . RemoveFirst ( ) ; Console . Write ( " ▁ " + first . left . data + " ▁ " + second . right . data ) ; Console . Write ( " ▁ " + first . right . data + " ▁ " + second . left . data ) ; if ( first . left . left != null ) { q . AddLast ( first . left ) ; q . AddLast ( second . right ) ; q . AddLast ( first . right ) ; q . AddLast ( second . left ) ; } } } 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 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . left . left = new Node ( 12 ) ; tree . root . right . left . right = new Node ( 13 ) ; tree . root . right . right . left = new Node ( 14 ) ; tree . root . right . right . right = new Node ( 15 ) ; tree . root . left . left . left . left = new Node ( 16 ) ; tree . root . left . left . left . right = new Node ( 17 ) ; tree . root . left . left . right . left = new Node ( 18 ) ; tree . root . left . left . right . right = new Node ( 19 ) ; tree . root . left . right . left . left = new Node ( 20 ) ; tree . root . left . right . left . right = new Node ( 21 ) ; tree . root . left . right . right . left = new Node ( 22 ) ; tree . root . left . right . right . right = new Node ( 23 ) ; tree . root . right . left . left . left = new Node ( 24 ) ; tree . root . right . left . left . right = new Node ( 25 ) ; tree . root . right . left . right . left = new Node ( 26 ) ; tree . root . right . left . right . right = new Node ( 27 ) ; tree . root . right . right . left . left = new Node ( 28 ) ; tree . root . right . right . left . right = new Node ( 29 ) ; tree . root . right . right . right . left = new Node ( 30 ) ; tree . root . right . right . right . right = new Node ( 31 ) ; Console . WriteLine ( " Specific ▁ Level ▁ Order ▁ " + " traversal ▁ of ▁ binary " + " tree ▁ is ▁ " ) ; tree . printSpecificLevelOrder ( tree . root ) ; } }