> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/498-diagonal-traverse.md).

# 498 Diagonal Traverse

Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.

**Example:**

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

**Note:**\
The total number of elements of the given matrix will not exceed 10,000.

**The Idea:** First grab the elements that make the 'L' as shown below. Then take the diagonal of each element. Reverse the diagonal as you go for each alternative diagonal.![](/files/-LoJIojp7eyJiuNCQHm9)

**Complexity:** O(n) time and O(1) space

```python
class Solution:
    def findDiagonalOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if not matrix:
            return []

        n_rows = len(matrix)
        n_cols = len(matrix[0])

        L = []
        for i in range(0, n_rows):
            L.append((i, 0))
        for j in range(1, n_cols):
            L.append((n_rows - 1, j))

        def diagonal_along(r, c, reverse):
            diagonal = []
            while r >= 0 and c < len(matrix[0]):
                diagonal.append(matrix[r][c])
                r -= 1
                c += 1
            if reverse:
                return list(reversed(diagonal))
            return diagonal

        sol = []
        reverse = False
        for x, y in L:
            sol += diagonal_along(x, y, reverse)
            reverse = not reverse

        return sol
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/498-diagonal-traverse.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
