Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Created April 23, 2020 20:47
Show Gist options
  • Select an option

  • Save pyrofolium/9246985b174e47e1db1bf38994489a2e to your computer and use it in GitHub Desktop.

Select an option

Save pyrofolium/9246985b174e47e1db1bf38994489a2e to your computer and use it in GitHub Desktop.
def traverse(x: List[Tuple[int, int]]) -> Optional[List[int]]:
if len(x) == 0:
return []
not_leaves = {l: True for (l, r) in x if r is not None}
leaves = {i: True for j in x for i in j if i not in not_leaves and i is not None}
new_x = [(l, None if r in leaves else r) for (l, r) in x if l in not_leaves]
recursion = traverse(new_x)
return [i for i in leaves] + recursion if len(new_x) < x and recursion is not None else None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment