I have a tree and It is not a binary tree, so I want to compare all of the nodes and return the largest one, using recursion. I am having a problem of how to keep track of it, as I can't put a global variable, as it has to be local... I guess... But then if the recursion goes it resets the local variable.
def tree_max(node):
max=1
if node.left == None and node.right == None:
if node.value>max:
max=node.value
return max
elif node.left == None and node.right != None:
return tree_max(node)
elif node.left != None and node.right == None:
return tree_max(node.left)
else:
return tree_max(node.left)
Any suggestions?
nonlocal, a mutable default argument, etc.).max; that's the name of a built-in function.