Back to problems Sign in to unlock
Leetcode
Medium
Matrices
Arrays
Simulation
Diagonal Traverse
Return all elements of a matrix in diagonal order, alternating direction on each diagonal.
Acceptance 0%
Problem Statement
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.
Input Format
- A 2D integer matrix
matwithmrows andncolumns. - The matrix may be rectangular, not necessarily square.
Output Format
- Return an array containing the matrix elements in diagonal traversal order.
Constraints
- All matrix elements are integers.
- Visit each cell exactly once.
Premium problem context
Unlock deeper context for this problem
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.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.