Reverse alternate levels of a perfect binary tree | C ++ program to reverse alternate levels of a binary tree ; A Binary Tree node ; A utility function to create a new Binary Tree Node ; Function to store nodes of alternate levels in an array ; Base case ; Store elements of left subtree ; Store this node only if this is a odd level node ; Function to modify Binary Tree ( All odd level nodes areupdated by taking elements from array in inorder fashion ) ; Base case ; Update nodes in left subtree ; Update this node only if this is an odd level node ; Update nodes in right subtree ; A utility function to reverse an array from index 0 to n - 1 ; The main function to reverse alternate nodes of a binary tree ; Create an auxiliary array to store nodes of alternate levels ; First store nodes of alternate levels ; Reverse the array ; Update tree by taking elements from array ; A utility function to print indorder traversal of a binary tree ; Driver Program to test above functions
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; struct Node { char data ; struct Node * left , * right ; } ; struct Node * newNode ( char item ) { struct Node * temp = new Node ; temp -> data = item ; temp -> left = temp -> right = NULL ; return temp ; } void storeAlternate ( Node * root , char arr [ ] , int * index , int l ) { if ( root == NULL ) return ; storeAlternate ( root -> left , arr , index , l + 1 ) ; if ( l % 2 != 0 ) { arr [ * index ] = root -> data ; ( * index ) ++ ; } storeAlternate ( root -> right , arr , index , l + 1 ) ; } void modifyTree ( Node * root , char arr [ ] , int * index , int l ) { if ( root == NULL ) return ; modifyTree ( root -> left , arr , index , l + 1 ) ; if ( l % 2 != 0 ) { root -> data = arr [ * index ] ; ( * index ) ++ ; } modifyTree ( root -> right , arr , index , l + 1 ) ; } void reverse ( char arr [ ] , int n ) { int l = 0 , r = n - 1 ; while ( l < r ) { int temp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = temp ; l ++ ; r -- ; } } void reverseAlternate ( struct Node * root ) { char * arr = new char [ MAX ] ; int index = 0 ; storeAlternate ( root , arr , & index , 0 ) ; reverse ( arr , index ) ; index = 0 ; modifyTree ( root , arr , & index , 0 ) ; } void printInorder ( struct Node * root ) { if ( root == NULL ) return ; printInorder ( root -> left ) ; cout << root -> data << " ▁ " ; printInorder ( root -> right ) ; } int main ( ) { struct Node * root = newNode ( ' a ' ) ; root -> left = newNode ( ' b ' ) ; root -> right = newNode ( ' c ' ) ; root -> left -> left = newNode ( ' d ' ) ; root -> left -> right = newNode ( ' e ' ) ; root -> right -> left = newNode ( ' f ' ) ; root -> right -> right = newNode ( ' g ' ) ; root -> left -> left -> left = newNode ( ' h ' ) ; root -> left -> left -> right = newNode ( ' i ' ) ; root -> left -> right -> left = newNode ( ' j ' ) ; root -> left -> right -> right = newNode ( ' k ' ) ; root -> right -> left -> left = newNode ( ' l ' ) ; root -> right -> left -> right = newNode ( ' m ' ) ; root -> right -> right -> left = newNode ( ' n ' ) ; root -> right -> right -> right = newNode ( ' o ' ) ; cout << " Inorder ▁ Traversal ▁ of ▁ given ▁ tree STRNEWLINE " ; printInorder ( root ) ; reverseAlternate ( root ) ; cout << " Inorder Traversal of modified tree " ; printInorder ( root ) ; return 0 ; }