My AC python solution


  • 0
    J
    def binaryTreePaths(self, root):
        def root2leaf(root):
            if not root:
                return []
            if not root.left and not root.right:
                return [[root.val]]
            if not root.right:
                return [[root.val]+item for item in root2leaf(root.left)]
            if not root.left:
                return [[root.val]+item for item in root2leaf(root.right)]
            left=root2leaf(root.left)
            right=root2leaf(root.right)
            return [[root.val]+item for item in root2leaf(root.left)]+[[root.val]+item for item in root2leaf(root.right)]
            
        if not root:
            return []
        t=root2leaf(root)
        res=[]
        for item in t:
            temp=str(item[0])
            if len(item)==1:
                pass
            else:
                for i in range(1,len(item)):
                    temp+="->"+str(item[i])
            res.append(temp)
        return res

  • 0

    @jiangyimin This is 2 long.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.