leetcode——257. 二叉树的所有路径

自己还是没做出来。。

class Solution:
    def binaryTreePaths(self, root: TreeNode) -> List[str]:
        if not root:
            return []
        res=[]
        def helper(root,temp):
            if not root.left and not root.right:
                res.append(temp+[str(root.val)])
            if root.left:
                helper(root.left,temp+[str(root.val)])
            if root.right:
                helper(root.right,temp+[str(root.val)])
        helper(root,[])
        return ['->'.join(a) for a in res]
执行用时 :48 ms, 在所有 python3 提交中击败了62.38%的用户
内存消耗 :13.8 MB, 在所有 python3 提交中击败了5.68%的用户
 
——2019.11.21

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/11908046.html
今日推荐