Traverse a given Matrix using Recursion (original) (raw)

Last Updated : 12 Jul, 2025

Given a matrix **mat[][] of size n x m, the task is to traverse this matrix using recursion.
**Examples:

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

**Input: mat[][] = [[11, 12, 13],
[14, 15, 16],
[17, 18, 19]]
**Output: 11 12 13 14 15 16 17 18 19

**Approach:

Below is the implementation of the above approach:

C++ `

//C++ program to traverse the matrix using recursion #include #include

using namespace std;

// Recursive function to traverse the matrix void traverse(vector<vector>& mat, int i, int j) {

// If the current position is the bottom-right 
  // corner of the matrix
if (i == mat.size() - 1 && j == mat[0].size() - 1) {
    cout << mat[i][j] << endl;
    return;
}

// Print the value at the current position
cout << mat[i][j] << " ";

// If the end of the current row has 
  // not been reached
if (j < mat[0].size() - 1) {
  
    // Move right
    traverse(mat, i, j + 1);
}

// If the end of the current column has been reached
else if (i < mat.size() - 1) {
  
    // Move down to the next row
    traverse(mat, i + 1, 0);
}

}

int main() { vector<vector> mat = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; traverse(mat, 0, 0); return 0; }

Java

//Java program to traverse the matrix using recursion import java.util.Arrays;

class GfG {

// Recursive function to traverse the matrix
static void traverse(int[][] mat, int i, int j) {
  
    // If the current position is the bottom-right 
    // corner of the matrix
    if (i == mat.length - 1 && j == mat[0].length - 1) {
        System.out.println(mat[i][j]);
        return;
    }

    // Print the value at the current position
    System.out.print(mat[i][j] + " ");

    // If the end of the current row has not
      // been reached
    if (j < mat[0].length - 1) {
      
        // Move right
        traverse(mat, i, j + 1);
    }
  
    // If the end of the current column has been reached
    else if (i < mat.length - 1) {
      
        // Move down to the next row
        traverse(mat, i + 1, 0);
    }
}

public static void main(String[] args) {
    int[][] mat = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
    traverse(mat, 0, 0);
}

}

Python

#Python program to traverse the matrix using recursion def traverse(mat, i, j):

# If the current position is the bottom-
# right corner of the matrix
if i == len(mat) - 1 and j == len(mat[0]) - 1:
    print(mat[i][j])
    return

# Print the value at the current position
print(mat[i][j], end=" ")

# If the end of the current row has not
# been reached
if j < len(mat[0]) - 1:
  
    # Move right
    traverse(mat, i, j + 1)
    
# If the end of the current column 
# has been reached
elif i < len(mat) - 1:
  
    # Move down to the next row
    traverse(mat, i + 1, 0)

mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] traverse(mat, 0, 0)

C#

//C# program to traverse the matrix using recursion using System;

class GfG {

// Recursive function to traverse the matrix
static void Traverse(int[,] mat, int i, int j) {
    int rows = mat.GetLength(0);
    int cols = mat.GetLength(1);

    // If the current position is the bottom-right
      // corner of the matrix
    if (i == rows - 1 && j == cols - 1) {
        Console.WriteLine(mat[i, j]);
        return;
    }

    // Print the value at the current position
    Console.Write(mat[i, j] + " ");

    // If the end of the current row has 
      // not been reached
    if (j < cols - 1) {
      
        // Move right
        Traverse(mat, i, j + 1);
    }
  
    // If the end of the current column
      // has been reached
    else if (i < rows - 1) {
      
        // Move down to the next row
        Traverse(mat, i + 1, 0);
    }
}

static void Main() {
  
    int[,] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    Traverse(mat, 0, 0);
}

}

JavaScript

//Javascript program to traverse the matrix using recursion function traverse(mat, i, j) { const rows = mat.length; const cols = mat[0].length;

// If the current position is the bottom-right
// corner of the matrix
if (i === rows - 1 && j === cols - 1) {
    console.log(mat[i][j]);
    return;
}

// Print the value at the current position
process.stdout.write(mat[i][j] + " ");

// If the end of the current row has not been reached
if (j < cols - 1) {

    // Move right
    traverse(mat, i, j + 1);
}
// If the end of the current column has been reached
else if (i < rows - 1) {

    // Move down to the next row
    traverse(mat, i + 1, 0);
}

}

const mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; traverse(mat, 0, 0);

`

**Time Complexity: O(N * M)
**Auxiliary Space: O(M), because of recursive calling