Skip to content

Binary Tree Zigzag Level Order Traversal

MediumTrees
Decode it first. Clues, constraints, pattern and plan before any code.

Given the root of a binary tree, return its values level by level, but alternate the reading direction: the first level left to right, the second right to left, the third left to right again, and so on.

Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.

[3, 9, 20, null, null, 15, 7]  →  [[3], [20, 9], [15, 7]]
[1, 2, 3, 4, 5, 6, 7]          →  [[1], [3, 2], [4, 5, 6, 7]]
[]                             →  []

Constraints

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