Traverse a given Matrix using Recursion
Given a matrix arr of size N x M, the task is to traverse this matrix using recursion.
Examples:
Input: arr[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
Input: M[][] = {{11, 12, 13},
{14, 15, 16},
{17, 18, 19}}
Output: 11, 12, 13, 14, 15, 16, 17, 18, 19Approach:
- Check If the current position is in the bottom-right corner of the matrix
- Print the value at that position
- End the recursion
- Print the value at the current position
- Check If the end of the current row has not been reached
- Move right
- Check If the end of the current column has been reached
- Move down to the next row
Below is the implementation of the above approach:
C++
#include <iostream>using namespace std;// Define the dimensions of the matrixconst int N = 3;const int M = 3;// Recursive function to traverse the matrixvoid traverse(int arr[][M], int i, int j){ // If the current position is the bottom-right corner of // the matrix if (i == N - 1 && j == M - 1) { // Print the value at that position cout << arr[i][j] << endl; // End the recursion return; } // Print the value at the current position cout << arr[i][j] << ", "; // If the end of the current row has not been reached if (j < M - 1) { // Move right traverse(arr, i, j + 1); } // If the end of the current column has been reached else if (i < N - 1) { // Move down to the next row traverse(arr, i + 1, 0); }}int main(){ // Define the matrix int arr[N][M] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Start the traversal from the top-left corner of the // matrix traverse(arr, 0, 0); return 0;} |
Java
public class MatrixTraversal { // Define the dimensions of the matrix private static final int N = 3; private static final int M = 3; // Recursive function to traverse the matrix private static void traverse(int[][] arr, int i, int j) { // If the current position is the bottom-right corner of // the matrix if (i == N - 1 && j == M - 1) { // Print the value at that position System.out.println(arr[i][j]); // End the recursion return; } // Print the value at the current position System.out.print(arr[i][j] + ", "); // If the end of the current row has not been reached if (j < M - 1) { // Move right traverse(arr, i, j + 1); } // If the end of the current column has been reached else if (i < N - 1) { // Move down to the next row traverse(arr, i + 1, 0); } } public static void main(String[] args) { // Define the matrix int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Start the traversal from the top-left corner of the // matrix traverse(arr, 0, 0); }} |
Python3
# Define the dimensions of the matrixN = 3M = 3# Recursive function to traverse the matrixdef traverse(arr, i, j): # If the current position is the bottom-right corner of # the matrix if i == N - 1 and j == M - 1: # Print the value at that position print(arr[i][j]) # End the recursion return # Print the value at the current position print(arr[i][j], end=", ") # If the end of the current row has not been reached if j < M - 1: # Move right traverse(arr, i, j + 1) # If the end of the current column has been reached elif i < N - 1: # Move down to the next row traverse(arr, i + 1, 0)# Define the matrixarr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]# Start the traversal from the top-left corner of the# matrixtraverse(arr, 0, 0)# This code is contributed by Prince Kumar |
C#
// C# program for the above approachusing System;public class Program{ // Define the dimensions of the matrix private const int N = 3; private const int M = 3; // Recursive function to traverse the matrix private static void Traverse(int[,] arr, int i, int j) { // If the current position is the bottom-right corner of // the matrix if (i == N - 1 && j == M - 1) { // Print the value at that position Console.WriteLine(arr[i, j]); // End the recursion return; } // Print the value at the current position Console.Write($"{arr[i, j]}, "); // If the end of the current row has not been reached if (j < M - 1) { // Move right Traverse(arr, i, j + 1); } // If the end of the current column has been reached else if (i < N - 1) { // Move down to the next row Traverse(arr, i + 1, 0); } } public static void Main() { // Define the matrix int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Start the traversal from the top-left corner of the // matrix Traverse(arr, 0, 0); }}// This code is contributed by Prince Kumar |
Javascript
// Define the dimensions of the matrixconst N = 3;const M = 3;let ans="";// Recursive function to traverse the matrixfunction traverse(arr, i, j){ // If the current position is the bottom-right corner of // the matrix if (i === N - 1 && j === M - 1) { // Print the value at that position ans =ans + arr[i][j]; // End the recursion return; } // Print the value at the current position ans =ans + arr[i][j]+", "; // If the end of the current row has not been reached if (j < M - 1) { // Move right traverse(arr, i, j + 1); } // If the end of the current column has been reached else if (i < N - 1) { // Move down to the next row traverse(arr, i + 1, 0); }}// Define the matrixconst arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];// Start the traversal from the top-left corner of the// matrixtraverse(arr, 0, 0); console.log(ans); |
Output
1, 2, 3, 4, 5, 6, 7, 8, 9
Time Complexity: O(N * M)
Auxiliary Space: O(M), because of recursive calling




.gif)

Please Login to comment...