leetcode Binary Tree Paths python

# 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.res=[]
        if root == None:
            return self.res
        def dfs(root,path):
            if root.left is None and root.right is None:
                self.res.append(path)
            if root.left:
                dfs(root.left,path+'->'+str(root.left.val))
            if root.right:
                dfs(root.right,path+'->'+str(root.right.val))
        dfs(root,str(root.val))
        return self.res

 

posted @ 2015-12-06 14:26  hao.ma  阅读(207)  评论(0编辑  收藏  举报