Simple PYTHON


  • 0
    V
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param {TreeNode} root
        # @return {string[]}
        def binaryTreePaths(self, root):
            return self.pathUtil(root, '')
        
        def pathUtil(self, root, curPath):
            if not root:
                return []
            if not root.left and not root.right:
                return [curPath + str(root.val)]
            
            curPath += str(root.val) + '->'
            return self.pathUtil(root.left, curPath) + self.pathUtil(root.right, curPath)
            
            
    

Log in to reply
 

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