Skip to content
Onsite

Binary Tree Zigzag Level Order Traversal

MediumP2Coding

Given the root of a binary tree, return its level-order traversal in zigzag order: the first level left to right, the second right to left, the third left to right again, and so on. Return one inner array per level; an empty tree gives [].

Example 1

Input
root = [3,9,20,null,null,15,7]
Output
[[3],[20,9],[15,7]]

Example 2

Input
root = [1]
Output
[[1]]

Example 3

Input
root = []
Output
[]

Constraints

  • 0 <= number of nodes <= 2000
  • -100 <= Node.val <= 100

Hints

Run checks the 3 visible tests, Submit all 8.