### 將遞迴函數改成迭代函數的流程

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...

#{{{
\begin{eqnarray}
F(1) &=& 0 \nonumber \\
F(2) &=& 1 \nonumber \\
F(n) &=& F(n - 1) + F(n - 2), \text{where } n \geq 3 \nonumber \\
\end{eqnarray}
}}}#

#### 更多的轉換例子

##### 階乘(Factorial)函數

#{{{
n! = 1 \times 2 \times 3 \times \dotsb \times (n - 1) \times n
}}}#

#{{{
0! = 1
}}}#

##### 二元搜尋(Binary Search)

https://magiclen.org/binary-search/

##### 二元搜尋樹(BST, Binary Search Tree)的搜尋

        7
/ \
/   \
/     \
/       \
3         9
/ \       / \
/   \     /   \
1     5   8     9
\   / \
2 4   6

### 動態規劃

https://magiclen.org/dynamic-programming-basic/

### 自行建立空間來模擬堆疊空間

###### 遞迴函數無回傳，遞迴呼叫只連續發生在尾端
void f(param1, param2, ..., paramN) {
// do something

f(a, b, ..., n); // recursive call 1
f(aa, bb, ..., nn); // recursive call 2
// ...
f(aaa, bbb, ..., nnn); // recursive call n
}

void f(param1, param2, ..., paramN) {
stack = Empty Stack;
push param1, param2, ..., paramN to stack

loop {
pop param1, param2, ..., paramN from stack, if pop nothing then break

// do something

push aaa, bbb, ..., nnn to stack // recursive call n
// ...
push aa, bb, ..., nn to stack // recursive call 2
push a, b, ..., n to stack // recursive call 1
}
}

https://magiclen.org/quick-sort/

###### 遞迴函數無回傳，遞迴呼叫只發生在前端
void f(param1, param2, ..., paramN) {
if condition {
f(a, b, ..., n); // recursive call
}

// do something
}

void f(param1, param2, ..., paramN) {
stack = Empty Stack;
push param1, param2, ..., paramN to stack

loop {
if condition {
push a, b, ..., n to stack // recursive call
param1 = a, param2 = b, ..., paramN = n
} else {
break;
}
}

loop {
pop param1, param2, ..., paramN from stack, if pop nothing then break

// do something
}
}
###### 遞迴函數無回傳，遞迴呼叫只連續發生在前端
void f(param1, param2, ..., paramN) {
f(a, b, ..., n); // recursive call 1
f(aa, bb, ..., nn); // recursive call 2
// ...
f(aaa, bbb, ..., nnn); // recursive call n

// do something
}

void f(param1, param2, ..., paramN) {
stack1 = Empty Stack;
stack2 = Empty Stack;
push param1, param2, ..., paramN to stack1

loop {
pop param1, param2, ..., paramN from stack1, if pop nothing then break

push a, b, ..., n to stack1 // recursive call 1
push aa, bb, ..., nn to stack1 // recursive call 2
// ...
push aaa, bbb, ..., nnn to stack1 // recursive call n

push param1, param2, ..., paramN to stack2
}

loop {
pop param1, param2, ..., paramN from stack2, if pop nothing then break

// do something
}
}

###### 遞迴函數無回傳，遞迴呼叫只發生在前端和尾端
void f(param1, param2, ..., paramN) {
if condition1 {
f(a, b, ..., n); // recursive call 1
}

// do something

if condition2 {
f(aa, bb, ..., nn); // recursive call 2
}
}

void f(param1, param2, ..., paramN) {
loop {
if condition1 {
f(a, b, ..., n); // recursive call 1
}

// do something

if condition2 {
param1 = aa, param2 = bb, ..., paramN = nn // recursive call 2
continue;
}

break;
}
}

void f(param1, param2, ..., paramN) {
stack = Empty Stack;

'outer: loop {
push param1, param2, ..., paramN to stack

loop {
if condition1 {
push a, b, ..., n to stack // recursive call 1
param1 = a, param2 = b, ..., paramN = n
} else {
break;
}
}

loop {
pop param1, param2, ..., paramN from stack, if pop nothing then break 'outer

// do something

if condition2 {
param1 = aa, param2 = bb, ..., paramN = nn // recursive call 2
continue 'outer;
}
}
}
}

###### 其它

        7
/ \
/   \
/     \
/       \
3         9
/ \       / \
/   \     /   \
1     5   8     9
\   / \
2 4   6

7開始找最左邊的子節點，找到1。將1放入陣列後，再看右邊的子節點22沒有子節點，將2放入陣列。再回到3這個節點，將3放入陣列後，再看右邊的子節點5。從5開始找最左邊的子節點，找到4，將4放入陣列。之後的步驟依此類推。