forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol2.cpp
41 lines (36 loc) · 787 Bytes
/
sol2.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
#include <iostream>
#include <list>
class Node{
public:
int data;
Node *left, *right;
};
Node *createNode(int _data){
Node *t = new Node;
t->data = _data;
t->left = t->right = NULL;
return t;
}
void BFS(Node *root){
std::list<Node *> Q;
if (root==NULL) return;
Q.push_back(root);
while (!Q.empty()){
Node* s = Q.front();
std::cout<<"Visited "<<s->data<<'\n';
Q.pop_front();
if (s->left!=NULL) Q.push_back(s->left);
if (s->right!=NULL) Q.push_back(s->right);
}
}
int main(){
Node *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
BFS(root);
return 0;
}