def visit_level(t, n, act):
"""
Visit each node of BinaryTree t at level n and act on it. Return the number of nodes visited visited.
"""
if t is None:
return 0
elif n == 0:
return 1
else:
return (visit_level(t.left, n-1, act) +
visit_level(t.right, n-1, act)
I'm struggling with the else part. I don't understand why it should be n-1 but not n instead. if n = 1, aren't we visit the tree at level 0? I guess the outcome would be 2 but not 3 as we expected.
Also, term test two is coming, and we have assignment two to due by two weeks. Hopefully I will be well prepared for the upcoming test and get started for the assignment.
No comments:
Post a Comment