Source: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

Given the root of a binary tree, return the zigzag level order traversal of its nodes values. (i.e., from left to right, then right to left for the next level and alternate between).

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
// O(N^2) time, O(N) space
var zigzagLevelOrder = function(root) {
// If the tree is empty, return empty array
if (root === null) {
return [];
}
// BFS with queue
const levels = [];
const queue = [];
queue.push(root);
let isLeftToRight = true;
while (queue.length > 0) {
const levelSize = queue.length;
const level = [];
let i = 0;
// For each node in the current level, we'll add the current node value and enqueue the children
while (i < levelSize) {
const currentNode = queue.shift();
level.push(currentNode.val);
if (currentNode.left !== null) {
queue.push(currentNode.left);
}
if (currentNode.right !== null) {
queue.push(currentNode.right);
}
i++;
}
// Depending on if we're going left to right or right to left, we'll push the nodes in a level from left to right or reverse the first and then toggle the direction
if (isLeftToRight) {
levels.push(level);
} else {
levels.push(level.reverse());
}
isLeftToRight = !isLeftToRight;
}
return levels;
};