3 SIMPLE TRICKS FOR RECURSION OVER A TREE STRUCTURE in JavaScript / NodeJS

There are a few simple tricks I use to help simplify the process of creating a recursive function.

Recursion doesn’t have to be difficult. These tricks, not only help you in writing and reasoning about a recursive function, but will also help reduce the complexity of that function.

Start with the exit cases — 01:26
Focus on the individual values and not the whole structure – 02:32
Write the towards the bottom — 04:40

As a BONUS, the Runkit also includes an example replacing the for loop with reduce

Runkit: https://runkit.com/joelnet/5f1344792ad936001ad53c94

Source: https://www.youtube.com/watch?v=uaLDj8uyXi0

Leave a Reply

Your email address will not be published. Required fields are marked *