-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelOrderTraversal.cpp
More file actions
62 lines (59 loc) · 1021 Bytes
/
Copy pathLevelOrderTraversal.cpp
File metadata and controls
62 lines (59 loc) · 1021 Bytes
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
58
59
60
61
#include "stdafx.h"
#include <conio.h>
#include <stdlib.h>
#include "tree.h"
#include "queue.h"
void LevelOrder(Node root)
{
Queue *q = new Queue();
q->EnQueue(root);
while(!q->IsEmpty())
{
static int currLevel = 1, nextLevel = 0;
Node temp = q->Dequeue();
printf("%d ",temp->value);
currLevel--;
if(temp->left)
{
q->EnQueue(temp->left);
nextLevel++;
}
if(temp->right)
{
q->EnQueue(temp->right);
nextLevel++;
}
if(currLevel == 0)
{
printf("\n");
currLevel = nextLevel;
nextLevel = 0;
}
temp->next = q->Head();
}
}
int main()
{
Node root = NULL;
root = Insert(root,6);
root = Insert(root,7);
root = Insert(root,9);
root = Insert(root,4);
root = Insert(root,2);
root = Insert(root,10);
root = Insert(root,5);
root = Insert(root,3);
root = Insert(root,1);
root = Insert(root,8);
PrintTree(root);
printf("\nLevel order:\n");
LevelOrder(root);
Node temp = root;
while(temp)
{
printf("%d ",temp->value);
temp = temp->next;
}
getch();
return 0;
}