title: How to maintain a global variable in recursive function in python?
body: <p>Unable to keep a track of the variable maxSum, after recursive calls it keeps resetting back to 0, despite passing it as a parameter in calls. The function maxPathSum should return 11 but it's returning 0.</p>
  <pre><code>class TreeNode:
      def __init__(self, val, left=None, right=None):
          self.val = val
          self.left = left
          self.right = right
  
  def maxPathSum(node):
      sums = []
      maxPath = []
      maxSum = 0
      recursiveSum(node, 0, maxSum, sums)
      return maxSum
  
  
  def recursiveSum(node, ssum, maxSum, sums):
      if node == None:
          return
      
      if maxSum &lt; ssum + node.val and node.left == None and node.right == None:
          maxSum = ssum + node.val
              
      sums.append(maxSum)
      
      # traverse left subtree
      recursiveSum(node.left, ssum + node.val, maxSum, sums)
      # traverse right subtree
      recursiveSum(node.right, ssum + node.val, maxSum, sums)
  
  
  root = TreeNode(1)
  root.left = TreeNode(2)
  root.right = TreeNode(3)
  root.left.left = TreeNode(4)
  root.left.right = TreeNode(5)
  root.right.left = TreeNode(7)
  root.right.right = TreeNode(6)
  print(maxPathSum(root))
  </code></pre>