Solución
solution.tsTypeScript
function sumBinaryTree(tree: (number | null)[], index: number = 0): number {
if (index >= tree.length || tree[index] === null) return 0
const node = tree[index]
// Node + left tree + right tree
return node
+ sumBinaryTree(tree, 2 * index + 1) // Left
+ sumBinaryTree(tree, 2 * index + 2) // Right
}
// No modificar: necesario para evaluar el resultado.
export { sumBinaryTree };
0respuestas