Recursion and Trees
75 / 170Recursion and TreesLesson 8 of 13About 36 min
Not every recursion sends answers upward. Some send context downward as a parameter.
The context being carried down is a list, given an empty list as its default. Two independent trees are walked, one after the other. Type both lines it prints.
class T: def __init__(self, val, left=None, right=None): self.val, self.left, self.right = val, left, right def values_down(node, seen=[]): if node is None: return seen seen.append(node.val) values_down(node.left, seen) values_down(node.right, seen) return seen print(values_down(T(1, T(2))))print(values_down(T(9)))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.