Skip to main content
LeetCode 498, Medium. Topics: Array, Matrix, Simulation. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

Problem

Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.

Examples

Diagonal Traverse

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 10^4
  • 1 <= m * n <= 10^4
  • -10^5 <= mat[i][j] <= 10^5

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on August 25, 2026