My function is as following
func helper(root: TreeNode?, _ result: [[Int]], _ list: [Int], _ sum: Int, _ total: Int) {
list.append(root!.val)
if(total + root!.val == sum && root?.left == nil && root?.right == nil) {
result.append(list)
}
if(root?.left != nil && sum < total + root!.val) {
helper(root!.left, result, list, sum, total + root!.val)
}
if (root?.right != nil && sum < total + root!.val) {
helper(root!.right, result, list, sum, total + root!.val)
}
}
When I tried to modify list
and result
it gave me errors
. I've searched that input
keyword can be used to pass parameters by reference, then it can be modified locally as well as externally, but I couldn't make the syntax correct. How can I make it work? Or there is any better way I can modify them?