-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralOrderTraversal.cpp
90 lines (82 loc) · 1.59 KB
/
SpiralOrderTraversal.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
84
85
86
87
88
89
90
//
// main.cpp
// inheritence
//
// Created by raghav babbar on 27/02/17.
// Copyright © 2017 raghav babbar. All rights reserved.
//in this type of then
// tehn class four has 2 copy of two variable one from class 2 and another from class 3
//by using virtual keyword
// if the base clas then the drive class may noit have may or maynit hav cinstructor
// the dash method of object addd or remove
// display n elert box when user cli
#include<iostream>
#include <stack>
using namespace std;
typedef struct tree
{
tree *right;
tree *left;
int data;
}tree;
tree *create(int d)
{
tree *ptr=new tree;
ptr->data=d;
ptr->left=ptr->right=NULL;
return ptr;
}
void insert(tree *&root,int data)
{
if(root==NULL)
root=create(data);
else if(data > root->data)
insert(root->right, data);
else if(data <root->data)
insert(root->left, data);
}
void spiralOrder(tree *root)
{
stack<tree*> s1;
stack<tree*> s2;
s1.push(root);
while(!s1.empty()||!s2.empty())
{
if(s2.empty())
{ while(!s1.empty())
{
tree *pop=s1.top();
cout<<" "<<pop->data;
s1.pop();
if(pop->left)
s2.push(pop->left);
if(pop->right)
s2.push(pop->right);
}
}
else if(s1.empty())
{
while(!s2.empty())
{
tree *pop=s2.top();
cout<<" "<<pop->data;
s2.pop();
if(pop->right)
s1.push(pop->right);
if(pop->left)
s1.push(pop->left);
}
}
}
}
int main()
{
tree *root=NULL;
while(1)
{int d;
cin>>d;
if(d==-1) break;
insert(root,d);
}
spiralOrder(root);
}