Why is this solution so god aweful?

import { TreeNode } from “./TreeNode”;
import {BinaryTree} from “./BinaryTree”;
import Deque from “./deque”;

function zigzagLevelOrder(root) {
if (root == null) return [];

let results = [],
    dq = new Deque(),
    reverse = false;

while (dq.length > 0) {
    let size = dq.length;

    for (let i = 0; i < size; i++) {
        if (reverse == false) {
            let node = dq.shift();
            results[results.length - 1].push(;
            if (node.left != null) {
            if (node.right != null) {
        } else {
            let node = dq.pop();
            results[results.length - 1].push(;

            if (node.right != null) dq.unshift(node.right);
            if (node.left != null) dq.unshift(node.left);
    reverse = !reverse;
return results;


// Driver code
function main() {
const input1 = [new TreeNode(100), new TreeNode(50), new TreeNode(200), new TreeNode(25), new TreeNode(75), new TreeNode(350)]
tree1 = new BinaryTree(input1);

const input2 = [new TreeNode(25), new TreeNode(50), new TreeNode(75), new TreeNode(100), new TreeNode(200), new TreeNode(350)]
tree2 = new BinaryTree(input2);

const input3 = [new TreeNode(350), new TreeNode(200), new TreeNode(100), new TreeNode(75), new TreeNode(50), new TreeNode(25)],
tree3 = new BinaryTree(input3);

tree4 = new BinaryTree([new TreeNode(100)]);

roots = [tree1.root, tree2.root, tree3.root, tree4.root, null];

for (let i = 0; i < roots.length; i++) {
    console.log(i + 1 + ".\tBinary Tree:");
    // Printing the in-order list using the method we just implemented
    console.log("\n\tThe zigzag level order traversal is: ", zigzagLevelOrder(roots[i]));
    console.log("\n", "-".repeat(100), "\n");



Course: Grokking Coding Interview Patterns in JavaScript - Learn Interactively
Lesson: Solution: Binary Tree Zigzag Level Order Traversal

1 Like

Hi @Mohammed_Mamun,

We apologize for any sort of inconveniences that you’re facing while working on the provided code. Could you please share more details about your concerns or specific aspects you found lacking? We aim to provide effective and clear solutions, and your feedback is crucial for us to understand how we can improve. We appreciate your input and are here to help address any questions or issues you might have.