1. 程式人生 > >python leetcode 257. Binary Tree Paths

python leetcode 257. Binary Tree Paths

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