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

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/85104850