0

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?

J_P
  • 642
  • 1
  • 8
  • 23
Xuting
  • 1
  • 1

2 Answers2

1

You should use the inout keyword in the function signature:

func helper(root: TreeNode?, inout _ result: [[Int]], inout _ list: [Int], _ sum: Int, _ total: Int)

and make the calls using "&" for example:

helper(root!.left, &result, &list, sum, total + root!.val)
oded betzalel
  • 213
  • 3
  • 10
0

You should be using inout, not input, as described here under the heading In-Out Parameters.

paulbailey
  • 5,328
  • 22
  • 35