The only thing changes is the level list. Use push or unshift based on the bool.

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[][]} */ var zigzagLevelOrder = function (root) { if (!root) return []; let isLeftToRight = true; let result = []; let queue = [root];

while (queue.length > 0) { const length = queue.length; let tmp = []; for (let i = 0; i < length; i++) { let first; first = queue.shift(); if (isLeftToRight) { tmp.push(first.val); } else { tmp.unshift(first.val); } if (first.left) queue.push(first.left); if (first.right) queue.push(first.right);