Medium · Matrices

Diagonal Traverse

Given an m × n matrix, return all of its elements in zigzag diagonal order: take the anti-diagonals i + j = 0, 1, 2 and onward in turn, reading even-numbered ones from bottom-left to top-right and odd-numbered ones from top-right to bottom-left.

Examples

Example 1

[[1,2,3],[4,5,6],[7,8,9]]

Output: [1,2,4,7,5,3,6,8,9]

Example 2

[[1,2],[3,4]]

Output: [1,2,3,4]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Matrices problems