Python (yield): all paths from leaves to root in a tree
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001
--
Chapters
00:00 Python (Yield): All Paths From Leaves To Root In A Tree
00:38 Answer 1 Score 1
00:55 Accepted Answer Score 7
01:19 Thank you
--
Full question
https://stackoverflow.com/questions/7134...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #yield
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001
--
Chapters
00:00 Python (Yield): All Paths From Leaves To Root In A Tree
00:38 Answer 1 Score 1
00:55 Accepted Answer Score 7
01:19 Thank you
--
Full question
https://stackoverflow.com/questions/7134...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #yield
#avk47
ACCEPTED ANSWER
Score 7
This code only yields leaves that are (immediate) children of the root. The other ones get visited, they yield to the upper function, but the upper function does nothing with them. What you need is to yield them from the lower function to the upper one:
def paths(self, acc=[]):
    if self.is_leaf():
        yield [self.node]+acc
    for child in self.children:
        for leaf_path in child.paths([self.node]+acc): # these two
            yield leaf_path                            # lines do that
This should do the trick.
ANSWER 2
Score 1
At the moment the for loop doesn't yield anything. It should instead yield all the elements that are generated by the recursive call:
for child in self.children:
    for path in child.paths([self.node]+acc):
        yield path