-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelordertraversal.cpp
53 lines (44 loc) · 1.02 KB
/
levelordertraversal.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
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
queue<struct node *> myqueue;
struct node{
int data;
struct node *l;
struct node *r;
};
struct node * newnode(int a){
struct node *newn = (struct node *)(malloc(sizeof(struct node)));
newn->data = a;
newn->l=NULL;
newn->r = NULL;
return newn;
}
void levelordertraversal(struct node * p){
if (p != NULL) {
cout<<p->data<<" ";
if (p->l != NULL) {
myqueue.push(p->l);
}
if (p->r != NULL) {
myqueue.push(p->r);
}
myqueue.pop();
levelordertraversal(myqueue.front());
}
}
int main(){
struct node * root = NULL;
root= newnode(1);
root->l = newnode(2);
root->r = newnode(3);
root->l->l = newnode(4);
root->l->r = newnode(5);
root->r->r = newnode(7);
root->r->l = newnode(6);
myqueue.push(root);
cout<<"..........level order traversal ........"<<endl;
levelordertraversal(root);
cout<<endl;
}