hardTreesPure DSA~25 min
Zigzag Level-Order Traversal
Render a tree level by level, but alternate the reading direction each level: left-to-right, then right-to-left, and so on.
Problem
Given the root of a binary tree, return its zigzag level-order traversal: the first level left-to-right, the next right-to-left, alternating thereafter.
Input
A binary tree root.
Output
A list of levels, each a list of values in the appropriate direction.
Constraints
- 0 <= number of nodes <= 2000
- -100 <= node value <= 100
- Levels alternate direction starting left-to-right.
Examples
Example 1
Input
root = [3,9,20,null,null,15,7]
Output
[[3],[20,9],[15,7]]
Level 1 left-to-right, level 2 right-to-left, level 3 left-to-right.
Example 2
Input
root = [1]
Output
[[1]]
A single level.