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])
- object DeepestNode extends App {
- def findDeepestNode(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, t))
- ).maxBy(_._1)._2
- }
- case _ => acc
- }
- loop(tree, (0, tree))._2
- }
- val tree = Tree(1, List(Tree(2, null)))
- println(findDeepestNode(tree).value)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement