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] =>
- x map { loop(_, (acc._1 + 1, subtree)) } maxBy { _._1 }
- case _ => acc
- }
- }
- assert(tree.childs != null)
- loop(tree, (0, tree))._2
- }
- val tree = Tree(10,
- List(Tree(5,
- List(Tree(6,
- List(Tree(9))),
- Tree(1))),
- Tree(4)))
- val t1 =
- Tree(10,
- List(Tree(5),
- Tree(6, List(Tree(7))
- )
- )
- )
- println(findDeepestTree(tree).value)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement