Skip to content

Instantly share code, notes, and snippets.

@sumew
Created April 14, 2020 03:37
Show Gist options
  • Save sumew/5bd9e79345b6b16682f151ffc2e62590 to your computer and use it in GitHub Desktop.
Save sumew/5bd9e79345b6b16682f151ffc2e62590 to your computer and use it in GitHub Desktop.
def postOrder[T,S](tree: Tree[T], f: T => S): Queue[S] = {
def loop(g: Tree[T], output: Queue[S]): Queue[S] = g match {
case Tree(v,rest) => rest.foldLeft(output){case (agg,node) => loop(node,agg)}.enqueue(f(v))
}
loop(tree,Queue.empty)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment