Stuck in Recursion. Please help!

Hello, I am a little confused wrt writting the return statement in questions solved using recursion.
I am able to come up with the logic but most of the times, it becomes a task for me to place the return statement at an appropriate position.
For instance, in the following question:
https://leetcode.com/problems/minimum-depth-of-binary-tree/
I thought of going through all possible paths and then putting the count of number of nodes in a respective path in the array called count. I then decided to return the minimum value from the array count to find the minimum depth of the tree. However, I am struggling with putting the return statement at appropriate position.
this is my code::
image
this is the output given by my code:
image
Firstly, I fail to understand why the value of c is not being passed to the next recursive call, and why is it setting to 0 every single time?

Secondly, I fail to understand why we are not adding return before the function call present in line number 23,26.
if i put the return statement before function calls at line 23 and 26, the console displays the following
image


Another question is the keys and rooms problem on leetcode
image
https://leetcode.com/submissions/detail/600120164/
https://leetcode.com/problems/keys-and-rooms/

I ran the code and saw the answer is being printed in the print statement but it is not being returned! Instead null is being returned. Please help with the return part in recursion, it is just so mind boggling and it is a big impediment for me incase or problem solving.
I am so confused. Please help. I am able to solve problems through trial and error when it comes to adding the return statement but I often times don't understand why it behaves the way it behaves!
my main queries include:

  1. when to add return statement associated with function calls in recursion
  2. how to preserve the value of a changed variable in recursion

Please help!

Comments (1)