📌📌Python3 || ⚡26 ms, faster than 97.08% of Python3
\n\nclass Solution:\n    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        if not root:\n            return []\n\n        queue 
![image](https://assets.leetcode.com/users/images/3ac0be42-4841-44a1-87f0-a32a71f4b453_1676781049.3290617.png)\n```\nclass Solution:\n    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        if not root:\n            return []\n\n        queue = deque([root])\n        result, direction = [], 1\n\n        while queue:\n            level = [node.val for node in queue]\n            if direction == -1:\n                level.reverse()\n            result.append(level)\n            direction *= -1\n\n            for i in range(len(queue)):\n                node = queue.popleft()\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n\n        return result\n```\n\nHere is a step-by-step description of how it works:\n1. The input to the function is a binary tree, represented by its root node.\n1. The function first checks if the root node is None, in which case it returns an empty list. This is a special case to handle the situation where the tree is empty.\n1. If the root node is not None, the function initializes a queue with the root node and a list to store the final result.\n1. The variable "direction" is also initialized to 1, which is used to alternate the direction of the level traversal between left-to-right and right-to-left.\n1. The function enters a loop that continues until the queue is empty.\n1. Inside the loop, a list "level" is created using a list comprehension. It contains the values of all the nodes in the current level of the tree.\n1. If the direction is -1, the list "level" is reversed, to produce the zigzag traversal effect.\n1. The list "level" is appended to the final result list "result".\n1. The "direction" variable is then multiplied by -1 to alternate the direction of the level traversal in the next iteration.\n1. A nested loop is used to iterate through all the nodes in the current level of the tree.\n1. If a node has a left child, it is added to the queue.\n1. If a node has a right child, it is added to the queue.\n1. The loop continues until all the nodes in the current level have been processed.\n1. The outer loop then repeats the above process for the next level of the tree, until the queue is empty.\n1. The final result list, containing the zigzag level order traversal of the binary tree, is returned.
['Breadth-First Search', 'Queue', 'Python', 'Python3']