```
class Solution:
stack = []
sol = []
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
if root == None:
return self.sol
self.stack.append(root.val)
if root.right == None and root.left == None:
s = ""
for element in self.stack:
s+="%d->" % (element)
s=s[:-2]
self.sol.append(s)
self.binaryTreePaths(root.left)
self.binaryTreePaths(root.right)
self.stack.pop()
return self.sol
```

This code fails on LeetCode in this way (input is **[1,2]**):

Expected

["1->2"], got["1", "1->2"]

On my computer the solution is correct. Any idea?