|
| 1 | +// Approach |
| 2 | +// The zigzagLevelOrder function takes the root node of the binary tree as input and returns an array of arrays representing the zigzag level order traversal of the tree nodes' values. |
| 3 | + |
| 4 | +// The function first checks if the root node is null and returns an empty array if it is. Otherwise, it initializes the result array to store the traversal results, sets the level variable to 0, and initializes the queue array with the root node. |
| 5 | + |
| 6 | +// The function then enters a loop that processes each level of the tree. Within the loop, the function first gets the size of the current level and initializes an empty levelValues array to store the values of the nodes in the level. |
| 7 | + |
| 8 | +// The function then enters a nested loop that processes each node in the level. Within the nested loop, the function dequeues the next node from the queue, adds its value to the levelValues array, and enqueues its child nodes (if any) onto the queue. |
| 9 | + |
| 10 | +// After processing all the nodes in the level, the function checks the level variable to determine whether to reverse the levelValues array or not. If the level variable is odd (i.e., the level is even-numbered), the function reverses the levelValues array. |
| 11 | + |
| 12 | +// Finally, the function adds the levelValues array to the result array and increments the level variable. The function repeats the loop until the queue is empty. |
| 13 | + |
| 14 | +// Once the loop has completed, the function returns the result array containing the zigzag level order traversal of the tree nodes' values. |
| 15 | + |
| 16 | +// Complexity |
| 17 | +// The time complexity of the zigzagLevelOrder function is O(n), where n is the number of nodes in the binary tree. This is because the function needs to visit each node in the tree once to generate the zigzag level order traversal. |
| 18 | + |
| 19 | +// The space complexity of the function is O(w), where w is the maximum width of the binary tree (i.e., the maximum number of nodes in any level of the tree). This is because the function uses a queue to store the nodes of each level as it traverses the tree. The size of the queue is equal to the number of nodes in the widest level of the tree. In the worst case, when the tree is a complete binary tree, the maximum width is (2^(h+1))-1, where h is the height of the tree. Therefore, the space complexity is O(2^h), which is exponential in the height of the tree. However, in most cases, the tree will not be a complete binary tree, and the space complexity will be much lower than the worst-case scenario. |
| 20 | + |
| 21 | +// Code |
| 22 | +/** |
| 23 | + * Definition for a binary tree node. |
| 24 | + * function TreeNode(val, left, right) { |
| 25 | + * this.val = (val===undefined ? 0 : val) |
| 26 | + * this.left = (left===undefined ? null : left) |
| 27 | + * this.right = (right===undefined ? null : right) |
| 28 | + * } |
| 29 | + */ |
| 30 | +/** |
| 31 | + * @param {TreeNode} root |
| 32 | + * @return {number[][]} |
| 33 | + */ |
| 34 | + |
| 35 | +// Time O(n) |
| 36 | +// Space O(w) |
| 37 | +function zigzagLevelOrder(root) { |
| 38 | + if (!root) return []; // Handle empty tree |
| 39 | + |
| 40 | + const result = []; |
| 41 | + let level = 0; |
| 42 | + let queue = [root]; |
| 43 | + |
| 44 | + while (queue.length) { |
| 45 | + const levelSize = queue.length; |
| 46 | + const levelValues = []; |
| 47 | + |
| 48 | + for (let i = 0; i < levelSize; i++) { |
| 49 | + const node = queue.shift(); |
| 50 | + |
| 51 | + // Add node value to levelValues array |
| 52 | + levelValues.push(node.val); |
| 53 | + |
| 54 | + // Add child nodes to queue |
| 55 | + if (node.left) queue.push(node.left); |
| 56 | + if (node.right) queue.push(node.right); |
| 57 | + } |
| 58 | + |
| 59 | + // Alternate between reversing and not reversing the levelValues array |
| 60 | + if (level % 2 === 1) levelValues.reverse(); |
| 61 | + result.push(levelValues); |
| 62 | + |
| 63 | + level++; |
| 64 | + } |
| 65 | + |
| 66 | + return result; |
| 67 | +} |
0 commit comments