Created
November 19, 2018 16:15
-
-
Save this-mkhy/83266afc1ab60b237325656d4fc7d982 to your computer and use it in GitHub Desktop.
This file contains 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 <bits/stdc++.h> | |
using namespace std; | |
struct node | |
{ | |
int data; | |
struct node *left; | |
struct node *right; | |
}; | |
struct node* newNode(int data) | |
{ | |
node *temp = new node; | |
temp->data = data; | |
temp->left = temp->right = NULL; | |
return temp; | |
} | |
void printLevelTraversal(node *root) | |
{ | |
if (root==NULL) | |
return; | |
queue<node *> q; | |
//Push Root | |
q.push(root); | |
while (q.empty()==false) | |
{ | |
//Print the front of queue and remove it from the queue | |
node *currNode = q.front(); | |
cout<<currNode->data<<" "; | |
q.pop(); | |
//If this element have left child, Enqueue left child | |
if (currNode->left!=NULL) | |
{ | |
q.push(currNode->left); | |
} | |
//If this element have right child, Enqueue right child | |
if (currNode->right!=NULL) | |
{ | |
q.push(currNode->right); | |
} | |
} | |
} | |
int main() | |
{ | |
/* | |
1 | |
/ \ | |
2 3 | |
/ \ | |
4 5 | |
//1 2 3 4 5 | |
*/ | |
node *root = newNode(1); | |
root->left = newNode(2); | |
root->right = newNode(3); | |
root->left->left = newNode(4); | |
root->left->right = newNode(5); | |
cout<<"Breadth-first-Search (BFS): Level Order traversal of binary tree is "<<endl; | |
printLevelTraversal(root); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment