Skip to content

Instantly share code, notes, and snippets.

@sumew
Created April 14, 2020 02:52
Show Gist options
  • Save sumew/0e1481077b6039e42c9cfa3205059cc9 to your computer and use it in GitHub Desktop.
Save sumew/0e1481077b6039e42c9cfa3205059cc9 to your computer and use it in GitHub Desktop.
def preOrder[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,c) => c.foldLeft(output.enqueue(f(v))){case (acc,n) => loop(n,acc)}
}
loop(tree,Queue.empty[S])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment