algorithmfunctionrecursioniteration# How to convert a recursion with multiple recursive calls into an iteration? I already know how to do it when there is only one recursive call

Here is how I would convert a recursive function that only has one recursive call (I'll use JavaScript)

```
function f(n){
if(n>0){
f(n-1);
doStuff(n); //this could be anything
}
}
```

Iterative form

```
function f(n){
let stack = [];
while(n>0){
stack.push(n);
n = n-1;
}
while(stack.length!=0){
doStuff(stack.pop());
}
}
```

But I don't know how to go about a function like this

```
function f(n){
if(n>0){
f(n-1);
f(n-2); //how to deal with this?
doStuff(n);
}
}
```

Solution

You could put a bit more information on the stack, like *what* to do with the value on the stack: perform `f`

, or perform `doStuff`

.

So then it can become like this:

```
function f(n) {
const stack = [[0, n]];
while (stack.length) {
const [state, n] = stack.pop();
if (state) {
doStuff(n);
} else if (n > 0) {
// Three tasks to perform -- stack in reverse order
stack.push([1, n], [0, n-2], [0, n-1]);
}
}
}
```

Note that this approach also works for your first example, where you only have one recursive call. The only statement that needs to change (in the above code) is the `push`

call:

```
stack.push([1, n], [0, n-1]);
```

- Uniformly randomly generate a vector of k unsigned ints that sums to N
- Mapping elementwise Tuple Using Template Function
- Find the longest prefix for a table of tokenized strings
- Kalman Filter Prediction Implementation
- My ALV Tree balance factors calculate incorrectly
- Infix to postfix left one parentheses at the end when expression is fully enclosed
- C++ quick sort algorithm
- Identify connected subnetworks (R-igraph)
- Merge Sort Function not working when size of vector is not 2^n
- Given an array a of n integers, and q queries, for each value from index l to index r, add x to the value
- Count mountain arrays
- Get the number of all possible combinations that give a certain product
- getting TLE in leetcode question 212- word search 2 using backtrcaking even after pruning, how do i optimize it more
- Maximizing the sum of Adjacent sum in an array
- Weighted random selection from array
- If f(n) = O(g(n)), is log(f(n)) = O(log(g(n))?
- Why is KNN slow with custom metric?
- Based on a condition, how to remove an item from the processed array and move it to another array?
- The simplest algorithm for poker hand evaluation
- Find longest repetitive sequence in a string
- Check if all elements in a list are identical
- Minimize sum of products of adjacent elements of an array
- Creating dijkstras algorithm and having issues in C language
- Sliding subarray beauty working on IDE but not on leetcode
- How to insert a new element into an array in C?
- Permutations without recursive function call
- Algorithms and Data structure
- python - prefix sum algorithm
- Converting a Person's Height from feet and inches to just inches C#
- Reversible "hash" function from 64-bit integer to 64-bit integer