Skip to content

Instantly share code, notes, and snippets.

@thrasibule
Created August 2, 2019 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thrasibule/f2e4b1d0059cd00840b567a2abc8c538 to your computer and use it in GitHub Desktop.
Save thrasibule/f2e4b1d0059cd00840b567a2abc8c538 to your computer and use it in GitHub Desktop.
def tree(p):
n = len(p) + 1
g = [0.] * (n-1)
h = [0.] * (n-1)
i = n - 2
while i >= 0:
l = 2 * i + 1
r = 2 * i + 2
if l >= n - 1:
g[i] = p[l - n]
else:
g[i] = g[l] + h[l]
if r >= n - 1:
h[i] = p[r - n]
else:
h[i] = g[r] + h[r]
i -= 1
return g, h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment