-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0257 [E] Binary Tree Paths
Code with Senpai edited this page Jan 14, 2022
·
1 revision
# 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
footer