The following code from Geeks for Geeks to calculate the maximum path sum in Binary Tree.
In the function findMaxSum()
they declared a variable as findMaxUtil.res
What does that means ?
I saw this question on SOF If function.variable
inside a function it means that the function is kind of an object.But in this example the function name and variable are out of the original function. Would someone explain that please with a clear example!
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def findMaxUtil(root):
if root is None:
return 0
l = findMaxUtil(root.left)
r = findMaxUtil(root.right)
max_single = max(max(l, r) + root.data, root.data)
max_top = max(max_single, l+r+ root.data)
findMaxUtil.res = max(findMaxUtil.res, max_top)
return max_single
def findMaxSum(root):
findMaxUtil.res = float("-inf") ## This line
findMaxUtil(root)
return findMaxUtil.res ## and this line