Easy understanding python solution


  • 0
    G
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def binaryTreePaths(self, root):
            """
            :type root: TreeNode
            :rtype: List[str]
            """
            self.ans=[]
            def helper(root,path):
                if not root:
                    return
                if not root.left and not root.right:
                    self.ans.append(path+str(root.val))
                    
                helper(root.left,path+str(root.val)+'->')
                helper(root.right,path+str(root.val)+'->')
            
            helper(root,'')
            return self.ans
                
            
            
    

Log in to reply
 

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