Read the Levels in a Zigzag Pattern
Implement zigzagLevels
You are given the root of a binary tree. Return its levels as lists of values, from the root level downwards, but read the levels in a zigzag: the root level from left to right, the second level from right to left, the third from left to right again, and so on. An empty tree gives an empty list.
A level-order traversal with a queue produces every level from left to right. You can reverse alternate rows afterwards, or track the direction and write each value directly into the position where it belongs.
Example 1:
Input: root = [10,4,13,2,6,11,15,1,3]
Output: [[10],[13,4],[2,6,11,15],[3,1]]
Example 2:
Input: root = [5,8,2]
Output: [[5],[2,8]]
Example 3:
Input: root = []
Output: []
+ 11 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100 - ●
−100 ≤ node.val ≤ 100 - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
Return one list per level, from the root level downwards. The root level is read left to right, the next level right to left, the next left to right again, and so on, alternating. An empty tree gives an empty list
root =