Skip to content

Create 0103-binary-tree-zigzag-level-order-traversal.js #3588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2024

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Aug 5, 2024

Solved binary-tree-zigzag-level-order-traversal in JS.

File(s) Added: 0103-binary-tree-zigzag-level-order-traversal.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/submissions/1345678024/


if(root) q.enqueue(root);

let isLeft = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use better names

/**
 * BFS
 * Time O(N) | Space O(N)
 * https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
 * @param {TreeNode} root
 * @return {number[][]}
 */
var zigzagLevelOrder = (root) => {
    const isEdgeBase = (root === null);
    if (isEdgeBase) return [];
    
    return search(root);/* Time O(N) | Space O(N) */
};

var search = (root, isZigZag = true, order = []) => {
    const queue = new Queue([ root ]);
    
    while (!queue.isEmpty()) {        /* Time O(N) */
        const levels = [];

        bfs(queue, isZigZag, levels); /* Time O(WIDTH) | Space O(WIDTH) */
        order.push(levels);           /*                 Space O(N) */
        isZigZag = !isZigZag;
    }
    
    return order;
}

const bfs = (queue, isZigZag, levels) => {
    for (let level = queue.size(); (0 < level); level--) {/* Time O(WIDTH) */
        const { left, val, right } = queue.dequeue();
    
        if (left) queue.enqueue(left);  /* Space O(WIDTH) */
        if (right) queue.enqueue(right);/* Space O(WIDTH) */
        
        levels.push(val);               /* Space O(N) */
    }

    if (!isZigZag) levels.reverse();
}

/**
 * DFS - Pre-Order
 * Time O(N) | Space O(N)
 * https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
 * @param {TreeNode} root
 * @return {number[][]}
 */
var zigzagLevelOrder = (root, level = 0, order = []) => {
    const isBaseCase = (root === null);
    if (isBaseCase) return [];

    return search(root, level, order);
}

var search = (node, level, order) => {
    const isEven = ((level % 2) === 0);
    const action = isEven ? 'push' : 'unshift';
    const newLevel = (order[level] ?? []);

    newLevel[action](node.val);
    order[level] = newLevel;

    return dfsPreOrder(node, level, order);
}

const dfsPreOrder = (node, level, order) => {
    const { left, right } = node;

    zigzagLevelOrder(left, (level + 1), order);  /* Time O(N) | Space O(N) */
    zigzagLevelOrder(right, (level + 1), order);/* Time O(N) | Space O(N) */

    return order;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at the updated code.
submission link for the proposed code: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/submissions/1388722933/

/**
 * BFS
 * Time O(N) | Space O(N)
 * https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
 * @param {TreeNode} root
 * @return {number[][]}
 */
var zigzagLevelOrder = (root) => {
    const isEdgeBase = (root === null);
    if (isEdgeBase) return [];
    
    return search(root);/* Time O(N) | Space O(N) */
};

var search = (root, isZigZag = true, order = []) => {
    const queue = new Queue([ root ]);
    
    while (!queue.isEmpty()) {        /* Time O(N) */
        const levels = [];

        bfs(queue, isZigZag, levels); /* Time O(WIDTH) | Space O(WIDTH) */
        order.push(levels);           /*                 Space O(N) */
        isZigZag = !isZigZag;
    }
    
    return order;
}

const bfs = (queue, isZigZag, levels) => {
    for (let level = queue.size(); (0 < level); level--) {/* Time O(WIDTH) */
        const { left, val, right } = queue.dequeue();
    
        if (left) queue.enqueue(left);  /* Space O(WIDTH) */
        if (right) queue.enqueue(right);/* Space O(WIDTH) */
        
        levels.push(val);               /* Space O(N) */
    }

    if (!isZigZag) levels.reverse();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aakhtar3 (tagging to draw attention to the proposed code)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aadil42 Your proposed code looks good. Spacing is also good for this one. Make sure to change your current submission file to reflect your updated code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ykhan799, I updated the code.

@Ykhan799 Ykhan799 merged commit 4516f72 into neetcode-gh:main Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants