<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.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> result; // To store the result
    if (!root) return result;   // Return empty result if the root is null
    
    queue<TreeNode*> q;         // Queue for level order traversal
    q.push(root);               // Start with the root node
    bool leftToRight = true;     // Flag to control zigzag direction
    
    while (!q.empty()) {
        int levelSize = q.size();  // Number of nodes in the current level
        vector<int> currentLevel;  // To store the nodes of the current level
        
        // Process all nodes in the current level
        for (int i = 0; i < levelSize; ++i) {
            TreeNode* currentNode = q.front();
            q.pop();
            currentLevel.push_back(currentNode->val); // Add node value to current level
            
            // Add the left and right children to the queue
            if (currentNode->left) q.push(currentNode->left);
            if (currentNode->right) q.push(currentNode->right);
        }
        
        // If we are traversing right to left, reverse the current level
        if (!leftToRight) {
            reverse(currentLevel.begin(), currentLevel.end());
        }
        
        result.push_back(currentLevel); // Add current level to result
        leftToRight = !leftToRight;     // Toggle the direction for the next level
    }
    
    return result;
    }
};
13400