Contents [0/5] |
Loop [1/5] |
Non-Nullable Recursion [2/5] |
Non-Nullable Recursion [3/5] |
Nullable Recursion [4/5] |
Nullable Recursion [5/5] |
(Click here for one slide per page)
Loop [1/5] |
|
Non-Nullable Recursion [2/5] |
Direct translation of loop. |
Non-Nullable Recursion [3/5] |
It's more typical to check base case first in a recursive function. Note that lines 12-13 and 19-20 look almost exactly the same!
The only difference is we're working with
Computes forwards A single function call both:
|
Nullable Recursion [4/5] |
We can get rid of the similar if check in the starter function by using nullable recursion!
Computes both forwards and backwards Two separate function calls
The receiver could be a call to the starter, or the helper
This pattern is really great with balanced trees (next quarter). |
Nullable Recursion [5/5] |
Easiest to write, once you understand it. We don't know if a new node will be created in the helper! Therefore, it's important that the helper function return something back to its caller, and the caller always update the appropriate pointer. Even if a pointer ends up not being changed, the caller doesn't know if it needs to be changed, so it must unconditionally do the assigment of the pointer! Some common mistakes:
|