I have the following recursive data structure and a method iterating over it. Whilst doing so it should add a unique number n
to each node e.g. its respective number in level order traversal of the tree.
var data = {
children: [
{ children: [ ... ] },
{ children: [ ... ] },
{ children: [ ... ] },
...
]
}
var process = function (node) {
node.children.forEach(child, function () {
process(child);
});
return node;
}
How can I achieve this without changes to the data structure and minimal changes to the processing function? Result of process(data)
should be
var data = {
n: 1
children: [
{ n: 2, children: [ ... ] },
{ n: 3, children: [ ... ] },
{ n: 4, children: [ ... ] },
...
]
}