Write a function that returns the sum of all nodes, including root
var nodes = {
value: 7,
left: { value: 1, left: null, right: null },
right: { value: 4, left: null, right: null }
};
Considering this, result should be equal to 12.
sumTheTreeValues = root => {
console.log(root.value);
if (root.left != null) {
sumTheTreeValues(root.left);
}
if (root.right != null) {
sumTheTreeValues(root.right);
}
};
If this code will log
7
1
4
How to return the sum of these numbers without having to pass new parameter ?
returna value instead of logging it...