Binary Tree Paths LeetCode solution in Python
1class Solution:2def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:34# DFS solution5output = []6stack = [(root, "")]78while stack:9node, path = stack.pop()10path += str(node.val)1112if not node.left and not node.right:13output.append(path)1415path += "->"16if node.left:17stack.append((node.left, path))18if node.right:19stack.append((node.right, path))2021return output