# 5 lines recursive Python

• ``````def binaryTreePaths(self, root):
if not root:
return []
return [str(root.val) + '->' + path
for kid in (root.left, root.right) if kid
for path in self.binaryTreePaths(kid)] or [str(root.val)]``````

• very nice. You always have nice and clean solution!

• Hi Stefan,
This is the second time I have seen you using recursion in python like this, the first time was in different ways to add parenthesis, where you did:

``````def diffWaysToCompute(self, input):
return [a+b if c == '+' else a-b if c == '-' else a*b
for i, c in enumerate(input) if c in '+-*'
for a in self.diffWaysToCompute(input[:i])
for b in self.diffWaysToCompute(input[i+1:])] or [int(input)]
``````

I am learning a lot from you. I just want to ask you if there are any tricks or guidelines for coming up with super concise recursion code like these?

• Sorry, I think it's mainly experience and obsession :-)

Though for Python in particular, I learned quite a bit by looking at solutions of others on CheckiO and by spending a month answering Python questions on StackOverflow and reading answers of others. And I experiment a lot, trying to improve my code further and further. Feedback for solutions I posted myself on those sites also helped.

The "different ways to add parenthesis" might actually be a bad example, as I compute the same `self.diffWaysToCompute(input[i+1:])` for every `a`. It might be better to do a for-loop for `i`, in which you first get all possibilities for `a` and `b` and only then combine them. Or use itertools.product. I say "might" because I'm not sure recomputation actually makes a real difference. Anyway, that would make the code longer, so I didn't. But that means my obsession with short/simple code might cause inefficiency there.

• Could you explain the syntax in your return statement? I've never seen two consecutive for statements like that.

• That's not two for statements. There's no for statement at all there. It's a list comprehension with two for clauses. If you don't know list comprehensions, you should definitely learn them, they're great. Follow that link for some good explanation and examples.

• Very good answer!! Could you please explain why "or [str(root.val)]" is necessary ? Thanks!

• Without that, I'd return an empty path list for leaf nodes. Which is wrong by itself, and it would also lead to every path list being empty.

• Thanks a lot! I always learn from your solution!

• Sorry,could you use more simple code to explain this algorithm.I can't unstande it clearly
Thank you!

• Technically, your solution is only 3 lines!!! Cool!!!
I came up with a similar solution (list comprehension), but yours is shorter. The "or [str(root.val)]" is great!

``````def binaryTreePaths(self, root):
if not root:
return []
if not root.left and not root.right:
return [str(root.val)]
return [str(root.val)+"->" + i for sub in (root.left, root.right)
for i in self.binaryTreePaths(sub) if sub]``````

• Nice work! Impressive.

I would recommend you replace str(root.val) + '->' + path by "%s->%s"%(str(root.val) ,path), which could improve its efficiency.

Thanks a lot

• Easy to understand Python

``````def rootToLeafPaths(self, root):
if not root: return []
if not root.left and not root.right: return [str(root.val)]
return [str(root.val) + '->' + i for i in self.rootToLeafPaths(root.left)] +
[str(root.val) + '->' + i for i in self.rootToLeafPaths(root.right)]
``````

• Though your code is short, it is not that understandable.

• @simonzheng1234 agree! This piece code is really annoying.

• @simonzheng1234 @jz678 Why? I think it's about as straight-forward as it gets. How would you in your opinion improve it?

• @StefanPochmann I'm sorry if I sound rude. I'm new to python, and not familiar with the syntax. I felt frustrated yesterday that's why I left this comment. Really sorry!

for kid in (root.left, root.right) if kid
for path in self.binaryTreePaths(kid)] or [str(root.val)]
Do you mind explaining these two statements?

• @jz678 Haha, I considered saying "Maybe you just don't know Python well?" but decided against it :-P

Anyway, learn about "list comprehensions", then it should be easy. I'm just going through the root's children and their paths and prepend the root to them.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.