4 lines python DFS


  • 6
    A
    class Solution:
        def binaryTreePaths(self, root):
            if not root: return []
            result= [ str(root.val)+"->" + path for path in self.binaryTreePaths(root.left)]
            result+= [ str(root.val)+"->" + path for path in self.binaryTreePaths(root.right)]
            return result or [str(root.val)]  # if empty return leaf itself
            
    

  • 0
    M

    Great solution!


Log in to reply
 

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