Python solution


  • 0
    T
    def binaryTreePaths(self, root):
        paths = []
        if root is None:
            return paths
        
        left_path = self.binaryTreePaths(root.left)
        right_path = self.binaryTreePaths(root.right)
        
        for path in left_path:
            paths.append(str(root.val) + '->' + path)
        for path in right_path:
            paths.append(str(root.val) + '->' + path)
            
        if root.left == None and root.right == None:
            paths.append(str(root.val))
        
        return paths

Log in to reply
 

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