Solution to Zigzag traversal