Recursion and Trees
79 / 170Recursion and TreesLesson 12 of 13About 52 min
The framing problem from Unit 1, now with structure instead of a flat list.
Two different trees are written down in preorder. One version records nothing for an absent child; the other writes a #. Type the four lines it prints.
class T: def __init__(self, val, left=None, right=None): self.val, self.left, self.right = val, left, right def bare(node): if node is None: return [] return [str(node.val)] + bare(node.left) + bare(node.right) def marked(node): if node is None: return ["#"] return [str(node.val)] + marked(node.left) + marked(node.right) left_leaning = T(1, T(2))right_leaning = T(1, None, T(2)) print(",".join(bare(left_leaning)))print(",".join(bare(right_leaning)))print(",".join(marked(left_leaning)))print(",".join(marked(right_leaning)))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.