LC 0257 [E] Binary Tree Paths - codewithsenpai/algos Wiki

# 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 binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        res = []
        
        def dfs(node, path):
            if not node.left and not node.right:
                res.append(path)
                
            if node.left:
                dfs(node.left, f'{path}->{node.left.val}')
                
            if node.right:
                dfs(node.right, f'{path}->{node.right.val}')
            
        dfs(root, f'{root.val}')
        
        return res