Created
July 28, 2019 19:15
-
-
Save ajinkyajawale14499/c464f2864a4a8baba8b6b49babdabfa8 to your computer and use it in GitHub Desktop.
Print level order traversal of Binary Tree using Two Queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <queue> | |
#include <bits/stdc++.h> | |
using namespace std; | |
// Node structure | |
struct Node | |
{ | |
int data; | |
Node *left, *right; | |
}; | |
// Utility Function to create new node of Binary Tree | |
Node *newNode( int data ) | |
{ | |
Node* temp = new Node; | |
temp->data = data; | |
temp->left = NULL; | |
temp->right = NULL; | |
return temp; | |
} | |
// function to print level order using two Queues | |
void LevelOrder(Node* root) | |
{ | |
queue<Node *> q1, q2; | |
if( root == NULL) | |
return; | |
q1.push(root); //Pushing first level node into first queue | |
// Executing loop till both the queues become empty | |
while( !q1.empty() || !q2.empty() ) | |
{ | |
while( !q1.empty() ) | |
{ | |
//pushing left child of current node on first queue into second queue | |
if(q1.front()-> right != NULL) | |
q2.push(q1.front()-> left); | |
//pushing right child of currrent node on first queue into second queue | |
if(q1.front()-> right != NULL) | |
q2.push(q1.front()-> right); | |
cout<< q1.front()->data <<" "; | |
q1.pop(); | |
} | |
cout<< " \n "; | |
while( !q2.empty() ) | |
{ | |
//pushing left child of current node in second queue into first queue | |
if(q2.front()-> left != NULL) | |
q1.push(q2.front()-> left); | |
//pushing right child of current node in second queue into first queue | |
if(q2.front()-> right != NULL) | |
q1.push(q2.front()-> right); | |
cout<< q2.front()->data << " "; | |
q2.pop(); | |
} | |
cout << "\n"; | |
} | |
} | |
//driver function | |
int main() { | |
Node *root = newNode(1); | |
root->left = newNode(2); | |
root->right = newNode(3); | |
root->left->left = newNode(4); | |
root->left->right = newNode(5); | |
root->right->right = newNode(6); | |
LevelOrder(root); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment