I have my tree which a want to traverse through, each subtree might have different length:
const Tree = [
{
data: true,
subTrees: [{
data: true, subTrees: [{ data: true, subTrees: [] }]
}]
},
{
data: true,
subTrees: [{
data: true, subTrees: []
}]
},
{
data: true,
subTrees: [{
data: true, subTrees: [{ data: true, subTrees: [{ data: true, subTrees: [] }] }]
}]
},
{
data: true,
subTrees: [{
data: true, subTrees: [{ data: true, subTrees: [] }]
}]
}
]
How do I loop through each and every subTree so I can access the data in each node?
This is what I have now:
function traverseTree(currentTree) {
for (let i = 0; i < currentTree.length; i++) {
// do something
traverseTree(currentTree[i])
}
}