Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.55 KB | None | 0 0
  1. package example
  2.  
  3. case class Tree(val value: Int, val childs: List[Tree])
  4.  
  5. object DeepestNode extends App {
  6.  
  7.   def findDeepestNode(tree: Tree): Tree = {
  8.     def loop(subtree: Tree, acc: (Int, Tree)): (Int, Tree) =
  9.       subtree.childs match {
  10.         case x: List[Tree] => {
  11.           (for (t <- x)
  12.             yield acc._1 -> loop(t, (acc._1 + 1, t))
  13.           ).maxBy(_._1)._2
  14.         }
  15.         case _ => acc
  16.       }
  17.  
  18.     loop(tree, (0, tree))._2
  19.   }
  20.  
  21.   val tree = Tree(1, List(Tree(2, null)))
  22.   println(findDeepestNode(tree).value)
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement