Skip to main content

Binary Tree Zigzag Level Order Traversal

MediumPremium

Given the root of a binary tree, return the values of its nodes level by level in zigzag order: the first level left to right, the next level right to left, and so on, alternating each level.

Trees in the examples and tests are written in level order, with null for a missing child.

Example 1

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

Example 2

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

Constraints

  • The tree has between 0 and 2000 nodes.
  • -100 <= Node.val <= 100