Zigzag Level Order
DESCRIPTION (credit Leetcode.com)
Given the root of a binary tree, return the zigzag level-order traversal of its nodes' values.
The output should be a list of lists containing the values of the nodes at each level. The first list should contain the value of the root, the second list should contain the values of the nodes at the second level from right to left, the third list should contain the values of the third level from left to right, and so on.
EXAMPLES
Example 1:
Input:
[1, 3, 4, null, 2, 7, null, 8]
Output: [[1], [4, 3], [2, 7], [8]]
Example 2:
Input:
[4, 2, 7, 1, 3, 6, 9, null, 5, null, 2]
Output: [[4], [7, 2], [1, 3, 6, 9], [2, 5]]
Run your code to see results here
Explanation
We should recognize that a level-order breadth-first traversal of the binary tree is the most straightforward way to solve this problem.
We'll use BFS to visit each node in the tree level by level, starting from the root node. For each level, we'll use a deque nodes_for_level to keep track of the nodes at that level. We'll also have a boolean left_to_right (initially True) that indicates the direction in which we should process the nodes at each level.
Whenever we reach a new level in the tree, we'll add the nodes in nodes_for_level to the output list, and toggle the value of left_to_right to prepare for the next level.
The diagram below shows the values of left_to_right and nodes_for_level for each level in the tree:
It's important to remember that the order in which we visit the nodes at each level using BFS is still from left to right. So the boolean left_to_right affects the order in which we add nodes to nodes_for_level, which we breakdown below:
Left to Right Processing
When left_to_right is true, we can simply add the nodes to the nodes_for_level list in the order they come off the BFS queue by adding them to the back of nodes_for_level.
Right to Left Processing
When left_to_right is false, the first node that we process at each level should be the last node in the nodes_for_level queue. We can ensure this is true by adding each new node to the front of the nodes_for_level list using the appendleft method of the deque data structure.
from collections import dequeclass Solution:def zig_zag(self, root):if not root:return []nodes = []queue = deque([root])left_to_right = Truewhile queue:level_size = len(queue)nodes_for_level = deque()# process all nodes at this levelfor i in range(level_size):node = queue.popleft()if left_to_right:# add the node to the back of the listnodes_for_level.append(node.val)else:# add the node to the front of the listnodes_for_level.appendleft(node.val)if node.left:queue.append(node.left)if node.right:queue.append(node.right)# we've processed all nodes at the current level# add them to the output list and toggle left_to_right# to prepare for the next levelnodes.append(list(nodes_for_level))left_to_right = not left_to_rightreturn nodes
Complexity Analysis
Time Complexity: O(N) where N is the number of nodes in the tree. We visit each node exactly once, and each node, we perform a constant amount of work, so the time complexity is O(N).
Space Complexity: O(N). The output nodes list contains each element in the tree exactly once, so the space complexity is O(N).
© 2024 Optick Labs Inc. All rights reserved.
Loading comments...