A plough crosses a field one row at a time and turns round at each end, so it runs left to right, then right to left, then left to right again. List a binary tree's levels the same way: return a list of lists in which the root's level reads left to right, the next level right to left, the one after that left to right, and so on.
Examples
10
/ \
4 12
/ \ \
1 6 15
Input: root = build_tree([10, 4, 12, 1, 6, None, 15])
Output: [[10], [12, 4], [1, 6, 15]]
Input: root = build_tree([1, 2, 3, 4, None, None, 5])
Output: [[1], [3, 2], [4, 5]]
Constraints
0 <= number of nodes <= 2000-10**4 <= node.val <= 10**4- Target complexity: O(n) time.
Goals
- Alternate the direction of every level
- Track the level index while doing BFS
- Keep the queue itself in normal left-to-right order