Zigzag function doesnt give any output

@Krishna-Singh-2678805765519156 wrong code for zigzag
algo for correct logic
We will be using 2 stacks to solve this problem. One for the current layer and other one for the next layer. Also keep a flag which indicates the direction of traversal on any level.

You need to pop out the elements from current layer stack and depending upon the value of flag push the childs of current element in next layer stack. You should maintain the output sequence in the process as well. Remember to swap the stacks before next iteration.
editorial code

i know the approach but i cant find any error with the program can you help me in it?

@Krishna-Singh-2678805765519156 i have corrected the segmentation fault but i dont think the logic will work correctly


just checked before pushing to next level

but i have done the way you have prescribed! used two stacks currentlevel and nextlevel and a flag variable. i am pushing root in current. and then used a while loop pushing children in next level and when current level becomes empty we swap current with next

@Krishna-Singh-2678805765519156 check your code for this input
50 true 290 true 12 false false false true 75 true 62 false false true 80 false false

2020-08-05-180153
this will be the tree

buts it’s giving segmentation fault on cb maybe some thing in the template is causing segmentation fault . try removing the template amd writing a basic code

comment the c_p_c() function it will work as normal. still showing wa

ohhh very silly mistake by me when i provided you the code corrected it

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.