Skip to content

Instantly share code, notes, and snippets.

@adam-nathan
Created February 28, 2022 16:39
Show Gist options
  • Save adam-nathan/0108314b23f05230bdea417b64a5720d to your computer and use it in GitHub Desktop.
Save adam-nathan/0108314b23f05230bdea417b64a5720d to your computer and use it in GitHub Desktop.
Eulerian tour algorithm
# Eulerian tour algorithm https://en.wikipedia.org/wiki/Eulerian_path
def eulerian_tour(root: TreeNode) -> List[TreeNode]:
tour = []
def dfs(node: TreeNode, depth: int) -> None:
tour.append((node.val, depth))
if node.left:
dfs(node.left, depth + 1)
tour.append((node.val, depth))
if node.right:
dfs(node.right, depth + 1)
tour.append((node.val, depth))
dfs(root, 0)
return tour
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment