File tree Expand file tree Collapse file tree 1 file changed +129
-0
lines changed Expand file tree Collapse file tree 1 file changed +129
-0
lines changed Original file line number Diff line number Diff line change @@ -164,3 +164,132 @@ int main()
164
164
getchar ();
165
165
return 0 ;
166
166
}
167
+
168
+
169
+ //Solved by Gouthaman Raaj, IT
170
+
171
+ //level order traversal of tree (question 30)
172
+
173
+ #include <stdio.h>
174
+
175
+ #include <stdlib.h>
176
+
177
+ struct node
178
+
179
+ {
180
+
181
+ int data ;
182
+
183
+ struct node * left ;
184
+
185
+ struct node * right ;
186
+
187
+ };
188
+
189
+ void printLevelOrder (struct node * root )
190
+
191
+ {
192
+
193
+ int i ;
194
+
195
+ int h = height (root );
196
+
197
+ for (i = 1 ; i <=h ; i ++ )
198
+
199
+ printGivenLevel (root , i );
200
+
201
+ }
202
+
203
+ void printGivenLevel (struct node * root , int level )
204
+
205
+ {
206
+
207
+ if (root == NULL )
208
+
209
+ {
210
+
211
+ printf ("traversal is completed" );
212
+
213
+ }
214
+
215
+ if (level == 1 )
216
+
217
+ printf ("%d " , root -> data );
218
+
219
+ else if (level > 1 )
220
+
221
+ {
222
+
223
+ printGivenLevel (root -> left , level - 1 );
224
+
225
+ printGivenLevel (root -> right , level - 1 );
226
+
227
+ }
228
+
229
+ }
230
+
231
+ int height (struct node * node )
232
+
233
+ {
234
+
235
+ if (node == NULL )
236
+
237
+ return 0 ;
238
+
239
+ else
240
+
241
+ {
242
+
243
+ int lheight = height (node -> left );
244
+
245
+ int rheight = height (node -> right );
246
+
247
+ if (lheight > rheight )
248
+
249
+ return (lheight + 1 );
250
+
251
+ else return (rheight + 1 );
252
+
253
+ }
254
+
255
+ }
256
+
257
+ struct node * newNode (int data )
258
+
259
+ {
260
+
261
+ struct node * node = (struct node * )
262
+
263
+ malloc (sizeof (struct node ));
264
+
265
+ node -> data = data ;
266
+
267
+ node -> left = NULL ;
268
+
269
+ node -> right = NULL ;
270
+
271
+ return (node );
272
+
273
+ }
274
+
275
+ void main ()
276
+
277
+ {
278
+
279
+ struct node * root = newNode (1 );
280
+
281
+ root -> left = newNode (2 );
282
+
283
+ root -> right = newNode (3 );
284
+
285
+ root -> left -> left = newNode (4 );
286
+
287
+ root -> left -> right = newNode (5 );
288
+
289
+ printf ("Level Order traversal of tree is \n" );
290
+
291
+ printLevelOrder (root );
292
+
293
+ }
294
+
295
+
You can’t perform that action at this time.
0 commit comments