forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Tree_Levelorder_Traversal.cpp
57 lines (46 loc) · 1017 Bytes
/
Tree_Levelorder_Traversal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int data;
node *left;
node *right;
};
node *newNode(int data)
{
node *new_node = new node;
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void LevelOrder(node * root)
{
queue <node *> que;
que.push(root);
while(!que.empty()){
if(que.front()->left!=NULL)
que.push(que.front()->left);
if(que.front()->right!=NULL)
que.push(que.front()->right);
cout << que.front()->data << " ";
que.pop();
}
}
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->left = newNode(6);
root->right->right = newNode(7);
cout << "Level Order traversal of tree is ";
LevelOrder(root);
return 0;
}
/* Output
Level Order traversal of tree is 1 2 3 4 5 6 7
*/