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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions javascript/0103-binary-tree-zigzag-level-order-traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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)
* }
*/
/**
* BFS | LevelOrderTraversal
* Time O(n) | Space O(n)
* https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
* @param {TreeNode} root
* @return {number[][]}
*/
var zigzagLevelOrder = function(root) {

const q = new Queue();

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.


const zigzagOrder = [];

while(!q.isEmpty()) {

let size = q.size();
const row = [];

while(size) {
const node = q.dequeue();
row.push(node.val);
if(node.left) q.enqueue(node.left);
if(node.right) q.enqueue(node.right);
size--;
}

if(!isLeft) {
zigzagOrder.push(row.reverse());
}
if(isLeft) {
zigzagOrder.push(row);
}
isLeft = !isLeft;
}

return zigzagOrder;
};