-1

For some context, this is the interview question I'm working on (taken from https://leetcode.com/problems/path-sum/description/). I now realize that its cleaner to just return a boolean value from each recursive function, but I still don't understand why this doesn't work:

class Solution:
    def hasPathSum(self, root, sum):
        result = None
        self.recurse(result, root, root.val, sum)
        return result
    def recurse(self, result, node, sub_sum, target):
        if sub_sum > target:
            return
        if sub_sum == target:
            result = True
            return
        if node.left:
            self.recurse(result, node.left, sub_sum+node.left.val, target)
        if node.right:
            self.recurse(result, node.right, sub_sum+node.right.val, target)

The value being returned (regardless of the tree depth) is None. Why isn't this value being changed? Isn't it passed by reference?

Jay K.
  • 546
  • 2
  • 10
  • 17
  • There's no such thing as `null` in Python, which implies that you're probably coming from a different language. If you tell us which language, it might be easier to explain it to you. – abarnert Sep 09 '18 at 20:51
  • 1
    Please post the entire question and not just the link to the question. Your `recurse` method never returns any value. Doing `result = True` just assigns `True` to the local value `result`. – Miket25 Sep 09 '18 at 20:52
  • 2
    All `result = True` does is make the local variable `result` into a name for `True`. It doesn't affect the value `result` used to be a name for (if anything) in any way. And, in fact, if `result` was `None` or `True`, those are immutable values; you _can't_ affect them in any way. – abarnert Sep 09 '18 at 20:52
  • 1
    No it is not passed by reference. See https://stackoverflow.com/questions/13299427/python-functions-call-by-reference – Red Cricket Sep 09 '18 at 20:56
  • @abarnert I meant None, my mistake. – Jay K. Sep 09 '18 at 21:03
  • The linked helped, got it. – Jay K. Sep 09 '18 at 21:05
  • @JayK. The fact that you think "passed by reference" is a meaningful thing that answers this question still implies that you're thinking in a different language, and again, if we knew what language that was, we'd know what to explain. The way Python variables are passed is the same thing Java docs call "by reference" and Ruby docs call "by value", but Python docs are smart enough to avoid using those terms because they just add confusion rather than clarification. – abarnert Sep 09 '18 at 21:05
  • I think your hesitation to use those terms is justified. I think one good way of explaining it is that Python argument passing works like assigning to local variable, one with no relation to other variables with the same name in other scopes. – juanpa.arrivillaga Sep 09 '18 at 21:27

1 Answers1

0

You can't affect result from outside the method it's defined in - it's just a local variable, scoped to the function you define it in. As others have said, there is no pass-by-reference happening here.

To achieve the effect you seem to be after, you'd have to have result being a property of the object - essentially refer to it as self.result instead. (You may also want to initialise it in your class's constructor function - but I'm not sure of exactly what the two methods shown here are supposed to do in the context of the whole class, so I can't be certain.)

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34