πŸ”„ Think of Recursion Like Stack Frames

When you call a function in Python, it gets added to a call stack.


πŸ“¦ Real-Life Analogy: Stack of Tasks

Imagine you're unpacking boxes, and each box has another box inside:

  1. Open Box 1 β†’ inside is Box 2
  2. Open Box 2 β†’ inside is Box 3
  3. Open Box 3 β†’ it's empty
  4. Now close Box 3, then Box 2, then Box 1 (in reverse)

🧠 This is Last In, First Out (LIFO) β€” like a stack.


🧠 Recursive Tree Example

Let’s use a very small tree to understand it fully:

markdown
CopyEdit
    1
   / \\
  2   3

Now let’s run your function dfs() on this tree.


πŸ” Visualizing Recursive Calls

python
CopyEdit
dfs(1)
β”œβ”€β”€ dfs(2)
β”‚   β”œβ”€β”€ dfs(None) β†’ 0
β”‚   └── dfs(None) β†’ 0
β”‚   return 1 + max(0, 0) = 1
β”œβ”€β”€ dfs(3)
β”‚   β”œβ”€β”€ dfs(None) β†’ 0
β”‚   └── dfs(None) β†’ 0
β”‚   return 1 + max(0, 0) = 1
return 1 + max(1, 1) = 2