-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathzigZagTraversal.cpp
58 lines (50 loc) · 1.07 KB
/
zigZagTraversal.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
54
55
56
57
58
#include <iostream>
#include <list>
#include "basicTreeTemplate.h"
using namespace std;
list<int> *adj = new list<int>[10];
static int i;
int zigZag(Node *root)
{
static int max = INT_MIN;
if (!root)
return 0;
adj[i].push_back(root->data);
i++;
if (i > max)
max = i;
zigZag(root->left);
zigZag(root->right);
i--;
return max;
}
int main()
{
Node *root = nullptr;
insertNode(root, 100);
insertNode(root, 200);
insertNode(root, 250);
insertNode(root, 150);
insertNode(root, 50);
insertNode(root, 20);
insertNode(root, 75);
insertNode(root, 10);
insertNode(root, 30);
insertNode(root, 60);
insertNode(root, 125);
insertNode(root, 175);
insertNode(root, 225);
insertNode(root, 300);
int max = zigZag(root);
list<int>::iterator itr;
for (int t = 0; t < max; t++)
{
if (t % 2 != 0)
adj[t].reverse();
for (itr = adj[t].begin(); itr != adj[t].end(); itr++)
{
cout << *itr << " -- ";
}
cout << endl;
}
}