Skip to main content

A rightleft-fold is an elegant solution too:

class Node (children:List[Node], value:Int) {
  def totalCost : Int = (value /: children) (_ + _.totalCost)
}

A right-fold is an elegant solution too:

class Node (children:List[Node], value:Int) {
  def totalCost : Int = (value /: children) (_ + _.totalCost)
}

A left-fold is an elegant solution too:

class Node (children:List[Node], value:Int) {
  def totalCost : Int = (value /: children) (_ + _.totalCost)
}
Source Link

A right-fold is an elegant solution too:

class Node (children:List[Node], value:Int) {
  def totalCost : Int = (value /: children) (_ + _.totalCost)
}