Level order traversal with direction change after every two levels | C # program to print Zig - Zag traversal in groups of size 2. ; A Binary Tree Node ; Function to print the level order of given binary tree . Direction of printing level order traversal of binary tree changes after every two levels ; For null root ; Maintain a queue for normal level order traversal ; Maintain a stack for printing nodes in reverse order after they are popped out from queue . ; sz is used for storing the count of nodes in a level ; Used for changing the direction of level order traversal ; Used for changing the direction of level order traversal ; Push root node to the queue ; Run this while loop till queue got empty ; Do a normal level order traversal ; For printing nodes from left to right , simply print the nodes in the order in which they are being popped out from the queue . ; For printing nodes from right to left , push the nodes to stack instead of printing them . ; for printing the nodes in order from right to left ; Change the direction of printing nodes after every two levels . ; Utility function to create a new tree node ; Driver Code ; Let us create binary tree
using System ; using System . Collections . Generic ; class GFG { public class Node { public Node left ; public int data ; public Node right ; } ; static void modifiedLevelOrder ( Node node ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { Console . Write ( node . data ) ; return ; } Queue < Node > myQueue = new Queue < Node > ( ) ; Stack < Node > myStack = new Stack < Node > ( ) ; Node temp = null ; int sz ; int ct = 0 ; bool rightToLeft = false ; myQueue . Enqueue ( node ) ; while ( myQueue . Count != 0 ) { ct ++ ; sz = myQueue . Count ; for ( int i = 0 ; i < sz ; i ++ ) { temp = myQueue . Peek ( ) ; myQueue . Dequeue ( ) ; if ( rightToLeft == false ) Console . Write ( temp . data + " ▁ " ) ; else myStack . Push ( temp ) ; if ( temp . left != null ) myQueue . Enqueue ( temp . left ) ; if ( temp . right != null ) myQueue . Enqueue ( temp . right ) ; } if ( rightToLeft == true ) { while ( myStack . Count != 0 ) { temp = myStack . Peek ( ) ; myStack . Pop ( ) ; Console . Write ( temp . data + " ▁ " ) ; } } if ( ct == 2 ) { rightToLeft = ! rightToLeft ; ct = 0 ; } Console . Write ( " STRNEWLINE " ) ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root . right . left . right . left = newNode ( 18 ) ; root . right . right . left . right = newNode ( 19 ) ; modifiedLevelOrder ( root ) ; } }