Two ways to approach Tree problems Recursive Leap of Faith o Identify base case(s)--what are you assuming exists? o Figure out how to break the problem down into a simpler one Going through python execution and reconstructing the Tree bottom- up o Whether or not you start here,should definitely use on exam or when you're debugging o Should use this as an iterative process with writing your code
Two ways to approach Tree problems ● Recursive Leap of Faith ○ Identify base case(s) -- what are you assuming exists? ○ Figure out how to break the problem down into a simpler one ● Going through python execution and reconstructing the Tree bottomup ○ Whether or not you start here, should definitely use on exam or when you’re debugging ○ Should use this as an iterative process with writing your code
One simpler example to walk through... def sum_branches(t): return t.label [sum_branches(b)for b in t.branches]
One simpler example to walk through... def sum_branches(t): return t.label + [sum_branches(b) for b in t.branches]
One simpler example to walk through... def sum_branches(t): return t.label +sum([sum_branches(b)for b in t.branches])
One simpler example to walk through... def sum_branches(t): return t.label + sum([sum_branches(b) for b in t.branches])
One simpler example to walk through... def sum_branches(t): return t.label +sum([sum_branches(b)for b in t.branches]) (leftmost branch...middle branch...rightmost branch)
One simpler example to walk through... def sum_branches(t): return t.label + sum([sum_branches(b) for b in t.branches]) (leftmost branch… middle branch… rightmost branch)
7 3 4 0 5 6 9 3 7 8 6 2
1 3 5 4 6 9 0 3 7 8 6 2