Skip to content
Onsite

Z Traversal of a Matrix

EasyP3Coding

Return the elements of an N x N matrix in a Z shape, as one array:

  1. The first row from left to right, stopping before the top-right cell.
  2. The anti-diagonal from the top-right cell to the bottom-left cell.
  3. The last row from the cell after the bottom-left corner to the right end.

Every visited cell appears once.

Example 1

Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[1,2,3,5,7,8,9]

Example 2

Input
matrix = [[1,2],[3,4]]
Output
[1,2,3,4]

Example 3

Input
matrix = [[5]]
Output
[5]

Constraints

  • 1 <= N <= 100
  • -10^9 <= matrix[i][j] <= 10^9

Hints

Run checks the 3 visible tests, Submit all 10.