Skip to interview prep
Apexed

92. Zigzag Level Order Traversal

MediumTrees

Return the values of a binary tree level by level, alternating direction: the first level left to right, the second right to left, and so on. Return one array per level.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees 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]]

Constraints

  • 0 ≤ nodes ≤ 2000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Zigzag Level Order Traversal — coding interview practice — Apexed