Skip to content
Advertisement

Node Tree break down as Array

is that possible to generate the array base one this node tree? That is the code that I have done. But I have no idea how to create 1,3,2 and 1,4 as an array. And I don’t know how to make it more efficient. is that possible to generate the array base one this node tree? That is the code that I have done. But I have no idea how to create 1,3,2 and 1,4 as an array. And I don’t know how to make it more efficient.

That is the array:

JavaScript
JavaScript

Expected Output

JavaScript
JavaScript

Advertisement

Answer

Your recursion strategy seems to be ok but you need to push another recursion call to stack by passing the children array if it is set. Next is to loop over each individual result subarray returned from the recursion call and current key name to it as shown below.

JavaScript

Demo: http://sandbox.onlinephpfunctions.com/code/bb826c6ce98976ce2b4c694946d79e765f4b6330

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