We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
As for the solution I mimick the C++ solution of this comment section.
Side Note: This is my solution in C and this is not the complete code to run it but only the interesting part. (I highly recommend to be able to deal with pointers)
#include<stdio.h> // Library for basic functions#include<stdlib.h> // String functions and memory allocations function// The presuming structure used in this exercisetypedefstructnode{char*data;structnode*left;structnode*right;}node;// Example of memory allocation functionnode*getNodeSpace(void){node*s=NULL;if((s=(node*)malloc(sizeof(node)))==NULL)exit(1);returns;}// The function in CvoidLevelOrder(node*root){inti=0;// allow to go through the storing arrayintx=0;// count the number of nodenode*temp;// node holdernode**base;// pointer to storing arraynode**s;// storing arrayif(root!=NULL){*s=root;base=s;++x;}while((temp=*(base+i))!=NULL&&x){printf("%s\n",temp->data);if(temp->left)if(*++s=getNodeSpace()){(*s)->data=temp->left->data;(*s)->left=temp->left->left;(*s)->right=temp->left->right;++x;}if(temp->right)if(*++s=getNodeSpace()){(*s)->data=temp->right->data;(*s)->left=temp->right->left;(*s)->right=temp->right->right;++x;}++i;--x;}}
See ya!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Level Order Traversal
You are viewing a single comment's thread. Return to all comments →
First your answer is in C not C++ which is different. By looking at your code I guess you're a beginner so I suggest you to read this : C Programming (second edition) by Brian and Dennis
And for pointers exclusively : Better understand of advanced pointers (C)
As for the solution I mimick the C++ solution of this comment section.
Side Note: This is my solution in C and this is not the complete code to run it but only the interesting part. (I highly recommend to be able to deal with pointers)
See ya!