breadFirst and contiguousSum in scala