Last active
September 22, 2023 13:50
-
-
Save jlcarrascof/ae67cc78f1e0251d94f9412a215b4535 to your computer and use it in GitHub Desktop.
Binary Tree Challenge - 22-09-2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function leftmostNodesSum(array) { | |
let sum = 0; | |
let index = 0; | |
while (index < array.length && array[index] !== 0) { | |
sum += array[index]; | |
index = 2 * index + 1; | |
} | |
return sum; | |
} | |
console.log(leftmostNodesSum([2, 7, 5, 2, 6, 0, 9])) | |
// => 11 | |
module.exports = leftmostNodesSum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment