More_recursion
def bigs(t):
"""Return the number of nodes in t that are larger than all their ancestors.
>>> a = Tree(1, [Tree(4, [Tree(4), Tree(5)]), Tree(3, [Tree(0, [Tree(2)])])])
>>> bigs(a)
4
"""
def f(a, x):
if a.label > x:
return 1 + sum([f(b, a.label) for b in a.branches])
else:
return sum([f(b, x) for b in a.branches])
return f(t,t.label - 1)def bigs(t):
"""Return the number of nodes in t that are larger than all their ancestors."""
n = 0
def f(a, x):
nonlocal n
if a.label > x:
n += 1
for b in a.branches:
f(b, max(a.label, x))
f(t,t.label - 1)
return n设计 functions
Last updated