Write a function that takes as input a binary tree dynamically implemented and print out the tree in $$\texttt{zig zag}$$ level order (i.e., from left to right, then right to left for the next level and alternate between). 

Example:

The zig zag level order output of the tree above is: 5 35 7 11 8 15 9 0

Hint: use two stacks: one for even levels and one for odd levels.


Difficulty level
Video recording
This exercise is mostly suitable for students
void printLevelOrderZigZag(Btree B)
{
	stack currentLevel, nextLevel;
	Btree currNode;
	currentLevel=CreateStack();
	nextLevel=CreateStack();

	Push(&currentLevel,B);
	while(!isEmptyStack(currentLevel))
	{
		while(Top(currentLevel,&currNode))
		{
			Pop(&currentLevel);
			if(currNode!=NULL)
				printf("%d ", currNode->data);
			if(currNode->left) Push(&nextLevel,currNode->left);
			if(currNode->right) Push(&nextLevel,currNode->right);
		}
		while(Top(nextLevel,&currNode))
		{
			Pop(&nextLevel);
			if(currNode!=NULL)
				printf("%d ", currNode->data);
			if(currNode->right) Push(&currentLevel,currNode->right);
			if(currNode->left) Push(&currentLevel,currNode->left);
		}
	}
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using the radix sort algorithm