I think there is a better solution https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/submissions/