# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def getPaths(self, node):
if node is None:
return []
if node and (node.left is None and node.right is None):
return [str(node.val)]
if node.left or node.right:
nodeValStr = str(node.val)
return [nodeValStr+"->"+x for x in self.getPaths(node.left)] + [nodeValStr+"->"+x for x in self.getPaths(node.right)]
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
return self.getPaths(root)