Return all elements of a matrix in diagonal order, alternating direction on each diagonal.
Given an matrix, visit every element exactly once and return the values in the order of a diagonal zigzag traversal.
Start from the top-left cell. For each diagonal, alternate between moving up-right and down-left. When the traversal reaches a boundary, move to the next valid starting cell for the following diagonal.
Your task is to produce the sequence of visited values.
mat with m rows and n columns.Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.