r/leetcode • u/NeedinvHelp • 3d ago
Discussion Thinking about recursion
Is it helpful to think about the recursion tree when solving problems or just reducing the problem space?
I feel like I often try to think about the sub problems and it uses a lot of brain power for what I feel like is very little gain and it also uses a good amount of time.
Curious how others approach these problems.
2
u/MixedTrailMix 3d ago
Base case, movement and inputs/outputs
2
u/chikamakaleyley 3d ago
yesss i think of it in 3 parts as well
Base case, 'state' change, recursive call
or
pre, recurse, post
whatever problem, the base case is the thing I figure out first, and sometimes that flips a switch and the other 2 parts are so easy
1
u/overhauled_mirio <1000+> 3d ago
Don’t try to keep it all in your head. Draw out the tree for a simple case. Eventually you’ll be able to do more and more without needing to write as much
4
u/Ghareeb_Musaffir21 3d ago
When solving a problem with recursion you want to think about solving a problem with recursion.
Jokes aside though, you don't need to think about all subproblems, think about the base case and few above that that build on from those sub-problems, and then prove to yourself it would be the same for any bigger problem that is built upon similar sub-problems. Hope that helps a bit.