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?