# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
res = []
if not root:
return res
stack = [(root, '')]
while stack:
node, road = stack.pop(0)
if node.left == None and node.right == None:
res.append(road + str(node.val))
if node.left:
stack.append((node.left, f'{road}{node.val}->'))
if node.right:
stack.append((node.right, f'{road}{node.val}->'))
return res
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
if not root:
return []
if not root.left and not root.right:
return [str(root.val)]
paths = []
if root.left:
for i in self.binaryTreePaths(root.left):
paths.append(str(root.val) + '->' + i)
if root.right:
for i in self.binaryTreePaths(root.right):
paths.append(str(root.val) + '->' + i)
return paths