Diagonal Traverse
Diagonal Traverse is LeetCode 498 (Medium). Given an m × n matrix mat, return all of its values in zigzag diagonal order. Start at the top-left cell, read the first diagonal going up and to the right, the next one going down and to the left, and keep alternating until the bottom-right cell.
- The matrix need not be square, and it can be a single row or a single column.
- It holds at most 10⁴ cells, so visiting each cell a constant number of times is plenty.
- m == mat.length
- n == mat[i].length
- 1 <= m, n <= 10⁴
- 1 <= m * n <= 10⁴
- -10⁵ <= mat[i][j] <= 10⁵
Intuition
Every cell on one of these diagonals has the same row + col. Moving up-right takes 1 off the row and adds 1 to the column, so the sum stays put. The sums run from 0 at the top-left to m + n − 2 at the bottom-right, one per diagonal.
So drop each cell into bucket number row + col. Scanning the matrix row by row fills every bucket from top to bottom, which is the down-left direction. The zigzag starts upward on diagonal 0, so reverse the even buckets, keep the odd ones, and join them in order.
This works for any diagonal traversal of a matrix: cells on one anti-diagonal share row + col, and cells on one main diagonal share row − col. Group cells by that number and the walking turns into list work.
Approach
Before reading on, write row + col inside each cell of a 3 × 3 grid and look at which cells share a number. Then decide which of those groups must be read backwards. Aim for O(m · n) time.
Make one bucket per diagonal
The sum row + col takes m + n − 1 values, from 0 to m + n − 2, so create that many empty lists. Every cell belongs to exactly one bucket, so no value is read twice or skipped.
Fill the buckets in row order
Scan the rows top to bottom and append mat[r][c] to bucket r + c. Each bucket receives its cells in increasing row order, so it already reads from its top-right end to its bottom-left end, the down-left direction.
Reverse every even diagonal
Diagonal 0 goes up, diagonal 1 goes down, and the directions keep alternating, so the even diagonals are the upward ones. Reverse those buckets so they start at the bottom-left, and leave the odd ones as they are.
Join the buckets in order
Extend the answer with bucket 0, then bucket 1, and so on. Each diagonal ends next to where the following one begins, so the joined list is the zigzag path the problem asks for.
Code in Python, C++ and Java
Common pitfalls
Reversing the odd diagonals
if d % 2 == 1:
if d % 2 == 0:
Row order fills every bucket going down-left, and diagonal 0 must go up, so the even buckets are the ones to flip. On [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the wrong parity returns [1, 4, 2, 3, 5, 7, 8, 6, 9] instead of [1, 2, 4, 7, 5, 3, 6, 8, 9].
Grouping by row − col
diags[r - c].append(mat[r][c])
diags[r + c].append(mat[r][c])
r − c is constant along the other kind of diagonal, top-left to bottom-right, and it goes negative, which Python quietly treats as counting from the end of the list. On [[1, 2], [3, 4]] it returns [4, 1, 3, 2] instead of [1, 2, 3, 4].
Edge cases
[[1], [2], [3]]Every bucket holds one cell, so reversing changes nothing and the answer is the column read top to bottom. A single row works the same way.
The middle diagonals are cut short by the top and bottom rows and never touch the left edge. Bucketing by row + col needs no rule for where a diagonal starts or ends, so these shapes need no extra code.
Complexity
Diagonal Traverse FAQ
Can it be done without the buckets?
Yes. Diagonal d has its top-right end at row 0, column d while d < n, and at row d − n + 1, column n − 1 after that. Step row + 1, col − 1 from there until you leave the grid, and append that run backwards when d is even. Beyond the answer, this needs only one diagonal's worth of memory.
Why do cell-by-cell walks break at the corners?
Many solutions move one cell at a time with an up or down flag, and at a wall step along it and flip the flag. The order of the wall checks matters. Moving up from the top-right cell, the walker is on the top row and the last column at once; testing "top row, so step right" first sends it off the grid, so "last column, so step down" must be tested first.