Reverse Level Order Traversal | A C ++ program to print REVERSE level order traversal using stack and queue This approach is adopted from following link tech - queries . blogspot . in / 2008 / 12 / level - order - tree - traversal - in - reverse . html http : ; A binary tree node has data , pointer to left and right children ; Given a binary tree , print its nodes in reverse level order ; Do something like normal level order traversal order . Following are the differences with normal level order traversal 1 ) Instead of printing a node , we push the node to stack 2 ) Right subtree is visited before left subtree ; Dequeue node and make it root ; Enqueue right child ; NOTE : RIGHT CHILD IS ENQUEUED BEFORE LEFT ; Enqueue left child ; Now pop all items from stack one by one and print them ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver program to test above functions ; Let us create trees shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; void reverseLevelOrder ( node * root ) { stack < node * > S ; queue < node * > Q ; Q . push ( root ) ; while ( Q . empty ( ) == false ) { root = Q . front ( ) ; Q . pop ( ) ; S . push ( root ) ; if ( root -> right ) Q . push ( root -> right ) ; if ( root -> left ) Q . push ( root -> left ) ; } while ( S . empty ( ) == false ) { root = S . top ( ) ; cout << root -> data << " ▁ " ; S . pop ( ) ; } } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return ( temp ) ; } int main ( ) { struct 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 ) ; cout << " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ STRNEWLINE " ; reverseLevelOrder ( root ) ; return 0 ; }