首页 > 编程语言 > 详细

leetcode Binary Tree Paths python

时间:2015-12-06 15:55:45      阅读:265      评论:0      收藏:0      [点我收藏+]
# 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

 

leetcode Binary Tree Paths python

原文:http://www.cnblogs.com/allenhaozi/p/5023600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!