498. Diagonal Traverse
Diagonal Traverse (LeetCode 498) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Set Matrix Zeroes (In-place Marking) subpattern.
How to solve Diagonal Traverse
Diagonal Traverse is a Set Matrix Zeroes In-Place problem, part of Array/Matrix Manipulation Patterns. Learn the in-place marking technique to set entire rows and columns to zero efficiently without extra space.
Work towards the solution in these steps:
- Try to visualize the traversal path and think about how elements are grouped along diagonals rather than rows or columns.
- Consider how the sum of the row and column indices (i + j) relates to the diagonals of the matrix.
- Notice that elements on the same diagonal share the same sum of indices, which can help you iterate through the matrix diagonally.
The reference solution runs in O(m * n) time and O(1) space.
The Diagonal Traverse solution and editorial walks through the full approach with reference code.
Problem statement
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation:
> Note: > The total number of elements of the given matrix will not exceed 10,000.
Topics and companies
Topics: nan.
Reported in interviews at Facebook, Amazon.
How to practise Diagonal Traverse on Thita.ai
Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(1) space.
Open Diagonal Traverse in the code editor, or read the Diagonal Traverse editorial for a worked solution with its approach and complexity analysis.
Where Diagonal Traverse sits in the DSA pattern sheet
- Array/Matrix Manipulation Patterns pattern guide
- Set Matrix Zeroes (In-place Marking) subpattern
- The full DSA patterns sheet
Problems related to Diagonal Traverse
Other problems that use the same Set Matrix Zeroes (In-place Marking) and Array/Matrix Manipulation Patterns techniques:
- Game of Life — Medium, same subpattern
- Set Matrix Zeroes — Medium, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Concatenation of Array — Easy, same pattern
- Contains Duplicate — Easy, same pattern
Diagonal Traverse FAQ
What is the LeetCode number of Diagonal Traverse?
Diagonal Traverse is LeetCode problem 498, rated Medium.
What pattern is used to solve Diagonal Traverse?
Diagonal Traverse is a Set Matrix Zeroes In-Place problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Set Matrix Zeroes (In-place Marking) subpattern.
What is the time complexity of Diagonal Traverse?
The reference solution runs in O(m * n) time and O(1) space.
Which companies ask Diagonal Traverse?
Diagonal Traverse has been reported in interviews at Facebook, Amazon.