Understanding a Recursive Problem
In this lesson, we will go deep into understanding a recursive problem.
Understanding the Problem
You should now have a clear understanding of how recursion works - what it is and when it can be used. Before you proceed writing your own recursive code, it is very important to understand the nitty-gritty details of how the code actually works.
Below is a basic example of how the order of execution for a recursive method changes at each successive recursive call.
Example
What do you think is the output of the code below? Take your time to think it over before you execute the code and look at the result.
Press + to interact
class ExampleClass {private static void printNum(int n) {// Base caseif (n == 0) {return;}// Recursive caseprintNum(n-1);System.out.print(n + " ");}public static void main( String args[] ) {// Recursive method called hereprintNum(6);}}
Code Explanation
If we read the method as it is written, we might expect the output to be ...