ruby code solution


  • 0
    M

    My ruby code:

    def sum(root, num_str, sum)
      return 0 unless root
    
      num_str += "#{root.val}"
    
      unless root.left || root.right
        sum += num_str.to_i
        return sum
      end
    
      left_sum = sum root.left, num_str, sum
      right_sum = sum root.right, num_str, sum
    
      left_sum + right_sum
    end
    
    def sum_numbers(root)
      return 0 unless root
    
      sum root, '', 0
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.