Zigzag Level Order Traversal
Read a tree level by level, switching direction on every level.
The problem
Implement Solution.zigzagLevelOrder(root). root is the root TreeNode of a binary tree, or None for an empty tree; each node has val, left and right. Return the values level by level, one inner list per level starting from the root. The root level reads left to right, the next level right to left, and the direction keeps switching on every level after that. Return [] for an empty tree. Trees are written in level order with None for a missing child. For [3, 9, 20, None, None, 15, 7] return [[3], [20, 9], [15, 7]]. Constraints: the tree has 0 to 2,000 nodes; values are integers from -100 to 100. Do not change the tree.
Starter code
class Solution:
def zigzagLevelOrder(self, root):
passPublic examples
root = [3, 9, 20, None, None, 15, 7]gives[[3], [20, 9], [15, 7]]root = [1, 2, 3]gives[[1], [3, 2]]root = [-9]gives[[-9]]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
