Skip to content

Commit cff6beb

Browse files
authored
Level order traversal in spiral form.cpp
1 parent a013a60 commit cff6beb

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Tree Node
5+
struct Node
6+
{
7+
int data;
8+
Node* left;
9+
Node* right;
10+
};
11+
// Utility function to create a new Tree Node
12+
Node* newNode(int val)
13+
{
14+
Node* temp = new Node;
15+
temp->data = val;
16+
temp->left = NULL;
17+
temp->right = NULL;
18+
19+
return temp;
20+
}
21+
22+
vector<int> findSpiral(Node *root);
23+
24+
// Function to Build Tree
25+
Node* buildTree(string str)
26+
{
27+
// Corner Case
28+
if(str.length() == 0 || str[0] == 'N')
29+
return NULL;
30+
31+
// Creating vector of strings from input
32+
// string after spliting by space
33+
vector<string> ip;
34+
35+
istringstream iss(str);
36+
for(string str; iss >> str; )
37+
ip.push_back(str);
38+
39+
// Create the root of the tree
40+
Node* root = newNode(stoi(ip[0]));
41+
42+
// Push the root to the queue
43+
queue<Node*> queue;
44+
queue.push(root);
45+
46+
// Starting from the second element
47+
int i = 1;
48+
while(!queue.empty() && i < ip.size()) {
49+
50+
// Get and remove the front of the queue
51+
Node* currNode = queue.front();
52+
queue.pop();
53+
54+
// Get the current node's value from the string
55+
string currVal = ip[i];
56+
57+
// If the left child is not null
58+
if(currVal != "N") {
59+
60+
// Create the left child for the current node
61+
currNode->left = newNode(stoi(currVal));
62+
63+
// Push it to the queue
64+
queue.push(currNode->left);
65+
}
66+
67+
// For the right child
68+
i++;
69+
if(i >= ip.size())
70+
break;
71+
currVal = ip[i];
72+
73+
// If the right child is not null
74+
if(currVal != "N") {
75+
76+
// Create the right child for the current node
77+
currNode->right = newNode(stoi(currVal));
78+
79+
// Push it to the queue
80+
queue.push(currNode->right);
81+
}
82+
i++;
83+
}
84+
85+
return root;
86+
}
87+
88+
89+
int main() {
90+
int t;
91+
string tc;
92+
getline(cin,tc);
93+
t=stoi(tc);
94+
while(t--)
95+
{
96+
string s;
97+
getline(cin,s);
98+
Node* root = buildTree(s);
99+
100+
vector<int> vec = findSpiral(root);
101+
for(int x : vec)
102+
cout<<x<<" ";
103+
cout << endl;
104+
}
105+
return 0;
106+
}
107+
108+
109+
// } Driver Code Ends
110+
111+
112+
/* A binary tree node has data, pointer to left child
113+
and a pointer to right child
114+
struct Node
115+
{
116+
int data;
117+
struct Node* left;
118+
struct Node* right;
119+
120+
Node(int x){
121+
data = x;
122+
left = right = NULL;
123+
}
124+
}; */
125+
126+
vector<int> findSpiral(Node *root)
127+
{
128+
//Your code here
129+
vector<int>v;
130+
if(root==NULL)
131+
return v;
132+
queue <Node *> q;
133+
stack<int>s;
134+
q.push(root);
135+
bool val=1;
136+
while(!q.empty())
137+
{
138+
int size=q.size();
139+
140+
for(int i=0;i<size;i++)
141+
{
142+
Node *cur=q.front();
143+
q.pop();
144+
if(val)
145+
s.push(cur->data);
146+
else
147+
v.push_back(cur->data);
148+
149+
if(cur->left)
150+
q.push(cur->left);
151+
if(cur->right)
152+
q.push(cur->right);
153+
}
154+
while(!s.empty())
155+
{
156+
v.push_back(s.top());
157+
s.pop();
158+
}
159+
val=!val;
160+
}
161+
return v;
162+
}

0 commit comments

Comments
 (0)