Join Nostr
2026-10-28 06:38:06 UTC

npub1u6…v0yg2 on Nostr: Learn: When recursion fits Use recursion when a problem contains smaller copies of ...

Learn: When recursion fits

Use recursion when a problem contains smaller copies of itself: trees, folders inside folders, divide and conquer, trying all options (backtracking).

Every recursive function needs a base case and a step toward it.

```csharp
Console.WriteLine(Factorial(5));
static long Factorial(int n) => n <= 1 ? 1 : n * Factorial(n - 1);
```

It prints 120. For very deep inputs, a loop is safer: each call uses stack memory.

Read more: https://www.geeksforgeeks.org/dsa/recursion-algorithms/

#tip · TIP-041