Skip to content

Instantly share code, notes, and snippets.

@nariaki3551
Last active September 14, 2022 02:41
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 nariaki3551/ec3b9a5f83157a734bea13d1e4575a8a to your computer and use it in GitHub Desktop.
Save nariaki3551/ec3b9a5f83157a734bea13d1e4575a8a to your computer and use it in GitHub Desktop.
depth = 3
tree = [i for i in range(2**depth-1)]
def path_generator(node_ix):
if node_ix >= 2**(depth-1) - 1: # leaf node
yield [node_ix]
else:
for path in _dfs(2*node_ix+1): # left child
yield [node_ix] + path
for path in _dfs(2*node_ix+2): # right child
yield [node_ix] + path
root_ix = 0
print(list(path_generator(root_ix)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment