-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Tree-5 Level Order Traversal Spiral.cpp
83 lines (73 loc) · 1.59 KB
/
GFG Tree-5 Level Order Traversal Spiral.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
#include <map>
#include <string.h>
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *left, *right;
};
struct node *newnode(int item)
{
struct node *temp = new node;
temp->data = item;
temp->left=temp->right = NULL;
return temp;
};
void printSpiralUtil(struct node *root, int l, int cl, int flag)
{
if(root)
{
if(cl==l)
{
cout<< root->data << " ";
return ;
}
if (flag ==0)
{
printSpiralUtil(root->left,l,cl+1,flag);
printSpiralUtil(root->right,l,cl+1,flag);
}
if (flag ==1)
{
printSpiralUtil(root->right,l,cl+1,flag);
printSpiralUtil(root->left,l,cl+1,flag);
}
}
}
int height(struct node *root)
{
if (!root)
return 0;
return max(height(root->left),height(root->right)) + 1;
}
printSpiral(struct node *root)
{
int iheight = height(root);
int flag = 1;
for(int i=0;i<iheight;i++)
{
printSpiralUtil(root,i,0,flag);
cout << endl;
if (flag==0)
{
flag=1; continue; }
if (flag==1)
{
flag=0;
}
}}
int main()
{
struct node* root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(7);
root->left->right = newnode(6);
root->right->left = newnode(5);
root->right->right = newnode(4);
printf("Spiral Order traversal of binary tree is \n");
printSpiral(root);
return 0;
}