const tree = [
{
v: 5,
c: [
{
v:10,
c: [
{
v:11,
}
]
},
{
v:7,
c: [
{
v:5,
c: [
{
v:1
}
]
}
]
}
]
},
{
v: 5,
c: [
{
v:10
},
{
v:15
}
]
}
]
function treeSum(tree) {
let sum = 0;
for (let i = 0; i < tree.length; i++) {
sum+= tree[i].v;
if (!tree[i].c) {
return tree[i].v;
}
sum += treeSum(tree[i].c)
}
/* tree.forEach((node,index)=>{
index;
sum += node.v
if (!node.c) {
return node.v;
}
sum += treeSum(node.c)
}) */
/* c foreach все работает*/
return sum
}
主页
/
user-515237