Recursion Flashcards

1
Q

What is recursion?

A

A programming technique where a function calls itself to solve a problem by breaking it into smaller versions of the same problem. Like solving a puzzle by solving smaller parts of the same puzzle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a base condition in recursion?

A

Factorial Example

The stopping condition that prevents infinite recursion. It’s the simplest case that can be solved directly without further recursive calls.
Example: In factorial(n), base case is when n=0 or n=1.

def factorial(n):
# Base case
if n == 0 or n == 1:
return 1
return n * factorial(n-1) #recursive case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When is it appropriate to use recursion?

A

Use recursion when:

  • Problem can be broken into smaller versions of itself
  • Solution involves tree-like or nested structures
  • Problems involve backtracking

Examples:
* Tree traversal
* Fibonacci sequence
* Directory traversal
* Divide and conquer algorithms

How well did you know this?
1
Not at all
2
3
4
5
Perfectly