Thereafter, we need to think about how to summarise the nodes. Just looking at it, it looks like we should summarise the top node and its two children. So, a code implementation would start off like this:
// tree-sum.jsconst root = require('./tree');function summarise(node) { return node.value + node.left.value + node.right.value;}console.log(summarise(root)) // 10
What happens if our tree grows and suddenly looks like this:
Let's add to the preceding code so it looks like this:
// example of a non recursive codefunction summarise(node) { return node.value + node.left.value + node.right.value + node.right.left.value + node.right.right.value ...