
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Level Order Traversal with Direction Change in C/C++
Level Order Traversal
This is one of the algorithms that processes or prints all nodes of a binary tree by traversing through depth, starting at the root and moving on to its children and so forth.
Example
INPUT ?

OUTPUT ?
2 4 7 3 6 11
This task involves printing a binary tree's level order traversal so that the first two levels are printed from right to left direction, and the next two levels from left to right direction, and so on. The challenge is that a binary tree's level order traverse must be reversed every two levels.
Refer to the example input for a better understanding of the problem.
INPUT ?

OUTPUT ?
2 3 7 11 12 6 5 19 23 16 13 9 21 17 18 1 20
In the above illustration, Level 1 and 2 are printed left to right, followed by Level 3 and 4, which are printed in reverse direction i.e. right to left, and Level 5 is printed from left to right.
Approach
We shall use both a queue and a stack to solve this problem. After every two levels, the stack is utilized to change the direction of traversal and the queue is utilized for normal-level order traversal.
The first two levels of a binary tree are printed using the standard level order traversal method.
We will declare two variables i.e. count=0 to store the level we are traversing and a boolean variable named rightToLeft=false for printing the element from right to left.
For every time we reach count=2, we will store !rightToLeft (not function) in rightToLeft and make count =0.
We push the nodes into the stack rather than printing them on the following two levels when rightToLeft=true. Once the nodes of the current level have been pushed out, we print the stack's nodes.
Because stack operates on LIFO, we can print the nodes in reverse direction by using stack.
For the next two levels, we will print nodes from left to right using the level order traversal method and then again use stack to print nodes from right to left for the following two levels and so forth.
By combining the use of both queue and stack, we can achieve the level order traversal that changes direction every two levels.
Example
#include <iostream> #include <bits/stdc++.h> using namespace std; class TreeNode{ public: int data; TreeNode*left; TreeNode*right; TreeNode (int d){ data=d; left=NULL; right=NULL; } }; void printEveryTwoLevelOrderTraversal(TreeNode* root){ //when the node is null if(root==NULL){ return; } // when root left and right is null if(root->left==NULL && root->right==NULL){ cout<<root->data<<endl; return; } queue<TreeNode*> Queue; // queue is for traversing the level stack<TreeNode*> Stack; // stack is for printing the node in reverse order once popped out from the queue int count=0; bool direction = false; Queue.push(root); // root node is pushed inside the queue while(!Queue.empty()){ int size=Queue.size(); count++; for(int i=0;i<size;i++){ TreeNode* tmp=Queue.front(); Queue.pop(); if(direction==false){ // print the node from left to right once the node is popped out from the queue cout<<tmp->data<<" "; } else{ // it stores the node in the stack for printing right to left Stack.push(tmp); } if(tmp->left!=NULL){ Queue.push(tmp->left); } if(tmp->right!=NULL){ Queue.push(tmp->right); } } if(direction==true){ while(!Stack.empty()){ TreeNode* tmp=Stack.top(); Stack.pop(); cout<<tmp->data<<" "; } } if(count==2){ // change the direction after every two level direction=!direction; count=0; } cout<<endl; } } int main(){ TreeNode* node=new TreeNode(5); node->left=new TreeNode(7); node->right=new TreeNode(8); node->left->left=new TreeNode(4); node->left->right=new TreeNode(9); node->left->left->left=new TreeNode(10); node->left->left->right=new TreeNode(14); node->right->left=new TreeNode(11); node->right->right=new TreeNode(12); printEveryTwoLevelOrderTraversal(node); return 0; }
OUTPUT
5 7 8 12 11 9 4 14 10
Time Complexity: O(n). The time complexity will be O(n) because each node is only traversed a maximum of twice during traversal.
Space Complexity: O(n) since the number of nodes in a binary tree will be the size of the stack or the queue.
In this article, we have tried to explain how to print a level order traversal that changes direction after every two levels. I hope that this article helped you to learn the concept.