forked from rahulkumarproc/Hackto21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelorderbfs.cpp
More file actions
52 lines (48 loc) · 1.1 KB
/
levelorderbfs.cpp
File metadata and controls
52 lines (48 loc) · 1.1 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
#include <bits/stdc++.h>
using namespace std;
struct treenode
{
int val;
struct treenode *left;
struct treenode *right;
treenode(int data)
{
val = data;
left = right = NULL;
}
};
void levelorder(treenode *root)
{
vector<int> vec;
if (!root)
return;
queue<treenode *> q;
q.push(root);
while (!q.empty())
{
treenode *temp = q.front();
vec.push_back(temp->val);
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
q.pop();
}
for (auto x : vec)
{
cout << x << "->";
}
}
int main()
{
treenode *root = new treenode(12);
root->left = new treenode(20);
root->right = new treenode(10);
root->left->left = new treenode(11);
root->left->right = new treenode(17);
root->right->left = new treenode(15);
root->right->right = new treenode(18);
cout << "level order:";
levelorder(root);
return 0;
}