- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0113-path-sum-ii.rb
32 lines (26 loc) · 788 Bytes
/
0113-path-sum-ii.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true
# 113. Path Sum II
# https://leetcode.com/problems/path-sum-ii
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @param {Integer} target_sum
# @return {Integer[][]}
defpath_sum(root,target_sum,path=[])
return[]ifroot.nil?
target_sum -= root.val
return[path + [root.val]]ifroot.left.nil? && root.right.nil? && target_sum.zero?
path_sum(root.left,target_sum,path + [root.val]) +
path_sum(root.right,target_sum,path + [root.val])
end
# **************** #
# TEST #
# **************** #
# TODO: Write tests