Skip to content
Advertisement

How to mirror an array recursively?

I have a tree from the youngest to the oldest

JavaScript

I need to mirror it (turn it inside out, if I may say so) to get such a structure

JavaScript

I’ve been working on this task for quite a long time and I can’t think of an algorithm to do it. What should I do to get the second option while having the first one?

Advertisement

Answer

This function will give you what you are looking for.

It is a recursive function that works its way down to the bottom of your list then progressively rebuilds it from the bottom up.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement