const tree = {
value: 10,
left: {
value: 20,
left: {},
right: {
value: 40,
left: {},
right: {},
},
},
right: {
value: 30,
left: {
value: 50,
left: {},
right: {},
},
right: {},
},
}
 
let count = 0;
 
function countValues(mainTree) {
const treeEntries = Object.entries(mainTree);
console.log(treeEntries)
for(let i = 0; i < treeEntries.length; i++) {
const isElObj = treeEntries[i][0] !== null && typeof treeEntries[i][0] === 'object' && treeEntries[i][0].constructor === Object
// if(!isElObj && treeEntries[i][0] === "value") {
// count = count + treeEntries[i][1]
// } else {
// countValues(arr[i])
// }
if(treeEntries[i].includes("value")) {
count = treeEntries[i][1]
}
}
return count
}
 
console.log(countValues(tree)); // Выведет 150
 
Code will run automatically after changes.
Code runs in an isolated environment.