Whilst the solution is clear, there’s a gotcha to take in consideration: JS accept circular references, hence you might end up on a stack overflow due to the recursion, or in an infinite loop if you avoid the recursion.
One solution, that comes at a cost ( memory ), might be using memoization.
Thanks for sharing your knowledge