Created
March 15, 2021 11:37
-
-
Save radiumrasheed/d144dc277f9f68de2a4a3def9be91982 to your computer and use it in GitHub Desktop.
Comparing Left and Right Branch of a Complete Binary Tree
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
const solution = (arr) => { | |
if (!arr) return ""; | |
if (arr.length === 0) return ""; | |
const sum = (arr, idx) => { | |
if (idx - 1 < arr.length) { | |
if (arr[idx - 1] === -1) return 0; | |
return arr[idx - 1] + sum(arr, idx * 2) + sum(arr, idx * 2 + 1); | |
} | |
return 0; | |
}; | |
const left = sum(arr, 2); | |
const right = sum(arr, 3); | |
return (left == right) ? "" : (left > right ? "Left" : "Right"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment