Skip to content

Instantly share code, notes, and snippets.

@radiumrasheed
Created March 15, 2021 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radiumrasheed/d144dc277f9f68de2a4a3def9be91982 to your computer and use it in GitHub Desktop.
Save radiumrasheed/d144dc277f9f68de2a4a3def9be91982 to your computer and use it in GitHub Desktop.
Comparing Left and Right Branch of a Complete Binary Tree
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