Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.64 KB | None | 0 0
  1. package example
  2.  
  3. case class Tree(val value: Int, val childs: List[Tree] = null)
  4.  
  5. object DeepestTree extends App {
  6.  
  7.   def findDeepestTree(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, subtree))
  13.           ).maxBy(_._1)._2
  14.         }
  15.         case _ => acc
  16.       }
  17.  
  18.     loop(tree, (0, tree))._2
  19.   }
  20.  
  21.   val tree = Tree(10,
  22.     List(Tree(5,
  23.         List(Tree(6,
  24.             List(Tree(9))),
  25.         Tree(1))),
  26.     Tree(4)))
  27.  
  28.   println(findDeepestTree(tree).value)
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement