<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[20,9],[15,7]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
	<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque
class Solution:
    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root:
            return []

        res = []
        # the level queue
        q = deque([(root, 0)])
        level_list = []
        while q:  
            node, lv = q.popleft()
            if lv == len(res):
                res.append([])
            if lv % 2 == 0:
                res[lv].append(node.val)
            else:
                res[lv] = [node.val] + res[lv]

            

            if node.left is not None:
                q.append((node.left, lv + 1))
            if node.right is not None:
                q.append((node.right, lv + 1))

        return res
                














        # output = []
        # def dfs(root, lv):
        #     if not root:
        #         return

        #     if lv <= len(output) - 1 and lv % 2 == 0:
        #         output[lv].append(root.val)

        #     elif lv <= len(output) - 1 and lv % 2 != 0:
        #         output[lv].insert(0, root.val)

        #     else:
        #         output.append([root.val])

        #     dfs(root.left, lv + 1)
        #     dfs(root.right, lv + 1)

        # dfs(root, 0)
        # return output
16800