Diagonal Traverse LeetCode Solution

Here, We see Diagonal Traverse LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Diagonal Traverse LeetCode Solution

Diagonal Traverse LeetCode Solution

Problem Statement

Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.

Example 1:

diag1 grid

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

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

Diagonal Traverse Leetcode Solution C++

class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
        vector<int> res;
        map<int, vector<int>> mp;
        for(int i = 0 ; i < mat.size() ; i++) 
            for(int j = 0 ; j < mat[0].size() ; j++)
                mp[i + j].push_back(mat[i][j]);
        for(auto i : mp) {
            if((i.first)%2 == 0) 
                reverse(i.second.begin(), i.second.end());
            for(auto k : i.second) res.push_back(k);
        }
        return res;
    }
};Code language: PHP (php)

Diagonal Traverse Leetcode Solution Java

class Solution {
    public int[] findDiagonalOrder(int[][] mat) {
        if (mat == null) {
            throw new IllegalArgumentException("Input matrix is null");
        }
        if (mat.length == 0 || mat[0].length == 0) {
            return new int[0];
        }
        int rows = mat.length;
        int cols = mat[0].length;
        int[] result = new int[rows * cols];
        int r = 0;
        int c = 0;
        for (int i = 0; i < result.length; i++) {
            result[i] = mat[r][c];
            if ((r + c) % 2 == 0) {
                if (c == cols - 1) {
                    r++;
                } else if (r == 0) {
                    c++;
                } else {
                    r--;
                    c++;
                }
            } else {
                if (r == rows - 1) {
                    c++;
                } else if (c == 0) {
                    r++;
                } else {
                    r++;
                    c--;
                }
            }
        }
        return result;
    }
}Code language: PHP (php)

Diagonal Traverse Solution JavaScript

var findDiagonalOrder = function(mat) {
    let rows = mat.length;
    let cols = mat[0].length;
    let result = new Array(rows + cols - 1).fill(null).map(() => []);
    for(let row = 0; row < rows; row++) {
        for(let col = 0; col < cols; col++) {
            if((row + col) % 2 === 0) result[row + col].unshift(mat[row][col]);
            else result[row + col].push(mat[row][col]);   
        }
    }
    return result.flat();
};Code language: JavaScript (javascript)

Diagonal Traverse Solution Python

class Solution(object):
    def findDiagonalOrder(self, mat):
        d={}
        for i in range(len(mat)):
            for j in range(len(mat[i])):
                if i + j not in d:
                    d[i+j] = [mat[i][j]]
                else:
                    d[i+j].append(mat[i][j])
        ans= []
        for entry in d.items():
            if entry[0] % 2 == 0:
                [ans.append(x) for x in entry[1][::-1]]
            else:
                [ans.append(x) for x in entry[1]]
        return ans
Scroll to Top