Created
February 28, 2022 16:39
-
-
Save adam-nathan/0108314b23f05230bdea417b64a5720d to your computer and use it in GitHub Desktop.
Eulerian tour algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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