Description

Given an m x n matrix, return all elements in diagonal order. Start from top-left, traverse diagonally alternating between up-right and down-left directions.

Examples

Input:mat = [[1,2,3],[4,5,6],[7,8,9]]
Output:[1,2,4,7,5,3,6,8,9]
Explanation:

Traverse diagonally alternating directions.

Input:mat = [[1,2,3,4]]
Output:[1,2,3,4]
Explanation:

For a single row matrix, diagonal traversal simply goes from left to right across the row, as each element forms its own diagonal.

Input:mat = [[1],[2],[3]]
Output:[1,2,3]
Explanation:

For a single column matrix, diagonal traversal goes from top to bottom down the column, as each element forms its own diagonal going in the downward direction.

Constraints

  • 1 ≤ m, n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!