Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example
- case class Tree(val value: Int, val childs: List[Tree] = null)
- object DeepestTree extends App {
- def findDeepestTree(tree: Tree): Tree = {
- def loop(subtree: Tree, acc: (Int, Tree)): (Int, Tree) =
- subtree.childs match {
- case x: List[Tree] => {
- (for (t <- x)
- yield acc._1 -> loop(t, (acc._1 + 1, subtree))
- ).maxBy(_._1)._2
- }
- case _ => acc
- }
- loop(tree, (0, tree))._2
- }
- val tree = Tree(10,
- List(Tree(5,
- List(Tree(6,
- List(Tree(9))),
- Tree(1))),
- Tree(4)))
- println(findDeepestTree(tree).value)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement