Diagonal Traverse

Given a grid with m rows and n columns, return all of its values in diagonal zig-zag order. Start at the top-left cell and move along the anti-diagonals (cells whose row and column add up to the same number). The first diagonal is just the corner; the second is read from top-right to bottom-left; the third from bottom-left to top-right; and so on, alternating until the bottom-right cell.

For a 3 x 3 grid holding 1 to 9 in row order, the answer is 1, 2, 4, 7, 5, 3, 6, 8, 9.

Constraints

  • 1 <= m, n <= 100
  • -10^5 <= matrix[i][j] <= 10^5

Examples

Example 1

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

The path is 1, then 2 4 going down-left, then 7 5 3 going up-right, then 6 8, then 9.

Example 2

Input: matrix = [[1,2,3],[4,5,6]]
Output: [1,2,4,5,3,6]

A 2 x 3 grid has four diagonals: 1 | 2 4 | 5 3 | 6.

Example 3

Input: matrix = [[1,2],[3,4],[5,6]]
Output: [1,2,3,5,4,6]

A 3 x 2 grid also has four diagonals: 1 | 2 3 | 5 4 | 6.

JavaScript you'll use: Arrays: create, read, update

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
function findDiagonalOrder(matrix) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.