class Solution:
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
res=[]
def dfs(root,s):
if not root.left and not root.right:
return res.append(s+str(root.val))
if root.left:
dfs(root.left,s+str(root.val)+'->')
if root.right:
dfs(root.right,s+str(root.val)+'->')
dfs(root,"")
return res
python leetcode 257. Binary Tree Paths
最新推荐文章于 2022-12-06 14:31:25 发布