Guest User

Scala solution for maximum-sum-path

a guest
Jul 25th, 2014
252
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sealed trait Tree
  2.  
  3. case object Empty extends Tree
  4.  
  5. case class Node(value: Int, left: Tree = Empty, right: Tree = Empty) extends Tree
  6.  
  7. object Tree {
  8.   def maxPath(tree: Tree): (Seq[Node], Int) = {
  9.       tree match {
  10.         case Empty => (Seq[Node](), 0)
  11.         case node @ Node(value, left, right) =>
  12.           val rl = maxPath(left)
  13.           val rr = maxPath(right)
  14.           val rm = if (rl._2 > rr._2) rl else rr
  15.           (node +: rm._1, node.value + rm._2)
  16.     }
  17.   }
  18. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×