1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# 257. Binary Tree Paths
"""
given the 'root' of a binary tree, return all root to leaf paths in any
order. a leaf is a node with no children
"""
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
ans, stack = [], [(root, "")]
while stack:
node, ls = stack.pop()
if not node.left and not node.right:
ans.append(ls + str(node.val))
if node.right:
stack.append((node.right, ls + str(node.val) + "->"))
if node.left:
stack.append((node.left, ls + str(node.val) + "->"))
return ans
if __name__ == "__main__":
obj = Solution()
print(obj.binaryTreePaths(root=[1, 2, 3, None, 5]))
|