擅长:python、mysql、java
<p>下面是一个可读性更强的递归生成器:</p>
<pre><code>def paths(node):
if node is None:
return
val, *children = node
if any(children):
for child in children:
for path in paths(child):
yield [val] + path
else:
yield [val]
>>> list(paths(root))
[[1, 2, 4, 7], [1, 2, 5], [1, 3, 6]]
</code></pre>
<p>这样做的另一个好处是,可以为具有任意数量子节点的节点工作。你知道吗</p>