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, 19
Approach: Below are the steps to traverse the Matrix recursively:
- Base Case: If the current row or column exceeds the size N or M, respectively, the recursion is stopped.
- If the current column exceeds the total number of columns M, then the next row is started.
if (current_col >= M) This row ends. Start for next row - If the current row exceeds the total number of rows N, then the complete traversal is stopped.
if (current_row >= N) Matrix has been traversed completely
- If the current column exceeds the total number of columns M, then the next row is started.
- Recursive Case: In each recursive call,
- The current element of the Matrix is printed.
- Two recursive calls are made:
- One to traverse the next row, and
- Another to traverse the next column.
After we print all the elements of the row and when we move on to the next column, this again calls the row in recursion, thereby printing all the elements of the matrix.
Below is the implementation of the above approach:
C++
// C++ program to traverse the matrix recursively #include <iostream> using namespace std; #define N 2 #define M 3 // Function to traverse the matrix recursively int traverseMatrix(int arr[N][M], int current_row, int current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix cout << arr[current_row][current_col] << ", "; // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code int main() { int arr[N][M] = { { 1, 2, 3 }, { 4, 5, 6 } }; traverseMatrix(arr, 0, 0); return 0; } |
Java
// Java program to traverse the matrix recursively class GFG { final static int N = 2; final static int M = 3; // Function to traverse the matrix recursively static int traverseMatrix(int arr[][], int current_row, int current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix System.out.print(arr[current_row][current_col] + ", "); // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code public static void main (String[] args) { int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; traverseMatrix(arr, 0, 0); } } // This code is contributed by AnkitRai01 |
C#
// C# program to traverse the matrix recursively using System; public class GFG { static int N = 2; static int M = 3; // Function to traverse the matrix recursively static int traverseMatrix(int [,]arr, int current_row, int current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix Console.Write(arr[current_row,current_col] + ", "); // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code public static void Main (string[] args) { int [,]arr = { { 1, 2, 3 }, { 4, 5, 6 } }; traverseMatrix(arr, 0, 0); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 program to traverse the matrix recursively N = 2 M = 3 # Function to traverse the matrix recursively def traverseMatrix(arr, current_row, current_col) : # If the entire column is traversed if (current_col >= M) : return 0; # If the entire row is traversed if (current_row >= N) : return 1; # Print the value of the current # cell of the matrix print(arr[current_row][current_col],end= ", "); # Recursive call to traverse the matrix # in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1 ) == 1) : return 1; # Recursive call for changing the # Row of the matrix return traverseMatrix(arr, current_row + 1, 0); # Driver code if __name__ == "__main__" : arr = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; traverseMatrix(arr, 0, 0); # This code is contributed by AnkitRai01 |
1, 2, 3, 4, 5, 6,
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Traverse the matrix in Diagonally Bottum-Up fashion using Recursion
- Traverse Linked List from middle to left-right order using recursion
- Count of ways to traverse a Matrix according to given conditions
- Traverse matrix in L shape
- Count of ways to traverse a Matrix and return to origin in K steps
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Count the number of ways to traverse a Matrix
- Shortest path to traverse all the elements of a circular array in increasing order
- C program to traverse an Array
- Decimal to Binary using recursion and without using power operator
- Add the given digit to a number stored in a linked list using recursion
- Count the occurrence of digit K in a given number N using Recursion
- Digital Root of a given large integer using Recursion
- Print even and odd numbers in a given range using recursion
- Create a Circular List Structure For Given Value K Using Recursion
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Sum of array elements using recursion
- Mean of array using recursion
- Remove duplicates from a sorted linked list using recursion
- Reverse a Doubly linked list using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01

