Simple recursive solution in Ruby.


  • 0
    M
    def has_path_sum(root, sum)
      return false if root.nil?
    
      if root.val == sum && root.left.nil? && root.right.nil?
        return true
      end
      
      has_path_sum(root.left, sum - root.val) || has_path_sum(root.right, sum - root.val)
    end

Log in to reply
 

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