def node_count(tree):
if is_leaf_node(tree):
return 0
count = 1
def inc_count(node): #node argument not used her, but needed in call
count += 1
tree_traversal(tree, inc_count)
return count
tree_traversal applies a function to each node in the tree and works fine. This method gives me: UnboundLocalError: local variable 'count' referenced before assignment
But this works:
def node_count(tree):
if is_leaf_node(tree):
return 0
l = []
def inc_count(node): #node argument not used her, but needed in call
l.append(1) #Add whatever
tree_traversal(tree, inc_count)
return len(l)
Why?
The last method works, but looks strange. Any other ways of doing it?