forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelorder_traversal.cpp
More file actions
57 lines (48 loc) · 1.25 KB
/
Copy pathLevelorder_traversal.cpp
File metadata and controls
57 lines (48 loc) · 1.25 KB
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 <bits/stdc++.h>
using namespace std;
// Defining the structure for nodes of the tree
struct BinaryTreeNode
{
int data;
struct BinaryTreeNode *left;
struct BinaryTreeNode *right;
BinaryTreeNode(int data)
{
this->data = data;
left = right = NULL;
}
};
void LevelorderTraversal(struct BinaryTreeNode *root)
{
queue<struct BinaryTreeNode *> q;
q.push(root);
while (!q.empty())
{
struct BinaryTreeNode *ptr = q.front();
q.pop();
cout << ptr->data << " ";
q.push(ptr->left);
q.push(ptr->right);
}
}
int main()
{
struct BinaryTreeNode *root = new BinaryTreeNode(10);
root->left = new BinaryTreeNode(20);
root->right = new BinaryTreeNode(30);
root->left->left = new BinaryTreeNode(40);
root->left->right = new BinaryTreeNode(50);
root->right->left = new BinaryTreeNode(60);
root->right->right = new BinaryTreeNode(70);
/*
-----------The formed tree will look like------
10
/ \
20 30
/ \ / \
40 50 60 70
*/
cout << "Level order Traversal of the tree : ";
LevelorderTraversal(root);
return 0;
}