Level Order Traversal (Zigzag) Problem

I’m trying to solve this problem with an approach similar to Level Order Traversal - Recursive approach. But 3 out of 4 test cases are showing Run Error. Help me identify the mistake.

Hi @anuragdeol2017
there is no need to make 2 functions and do… refer this https://ide.codingblocks.com/s/617820 simply pass a variable to to check level is odd or even

okay. understood that point of not making two separate functions. But other than that what is causing the wrong output. I think making two functions is extra work but that is not contributing to wrong output. Or is it?

I want you to point out, what in my code is causing wrong output.

Hi @anuragdeol2017
sry for delayed reply… there was just a minor mistake in ur code… ur base was missing
ie
if(root == NULL) {
return;
}
just add this to both functions… ur code will work fine…

corrected code --> https://ide.codingblocks.com/s/618863

yes all the test cases passed now.
But i have a doubt, why is this condition necessary. It means there can be a case where, level K is not present in tree, right??

yes… correct anurag…

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.