-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
|
||
if(root) q.enqueue(root); | ||
|
||
let isLeft = true; |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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();
}
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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/