4 ms·
The way I explain implementing recursion is this: 1. Write down what your function does in a comment. (e.g., "sum all the elements of a binary tree"). 2. Write
by DonaldPShimoda 7y ago
The way I explain implementing recursion is this:
1. Write down what your function does in a comment. (e.g., "sum all the elements of a binary tree").
2. Write the function signature.
3. Write the base case. (This is usually straightforward and most people don't seem to have an issue with it when reminded.)
4. Stop thinking.
5. Assume your function already works and write the recursive case.
6. Profit!
I find where most people get confused over recursion is in trying to reason about the recursive case. They think "Well if I start at the root of the tree, then the next thing I need is to get the children... and then... with the first child... uh..."
They get lost in the recursion. So I tell them: assume your function already works. Trust that you will write it correctly eventually. What does your function do? How can you use that?
If you assume you already have a function that sums binary trees, and if you're trying to sum the children of a branch node, well then use that function that you already have to get the sum of each of those children trees, and add up the results!
I always recommend to never try to visualize N levels of recursion, unless you're working through an example to actually test your implementation. Worrying about the deeper recursion is how people get lost and confused. Keep it simple: just look at the one layer and assume the other layers will do their job correctly.