> For the complete documentation index, see [llms.txt](https://garylai.gitbook.io/algorithm-and-data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garylai.gitbook.io/algorithm-and-data-structure/problems/array/diagonal-traverse.md).

# 498. Diagonal Traverse

[498. Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/)

```python
class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:

        if not mat or not mat[0]:
            return []

        R = len(mat)
        C = len(mat[0])

        res = []
        tmp = []
        for i in range(R + C - 1):
            tmp.clear()

            r = 0 if i < C else i - C + 1
            c = i if i < C else C - 1

            while r < R and c > -1:
                tmp.append(mat[r][c])
                r += 1
                c -= 1

            if i % 2 == 0:
                res.extend(tmp[::-1])
            else:
                res.extend(tmp)

        return res
```
