leetcode(257):Binary Tree Paths

题目:
Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
 /   \
2     3
 \
  5

All root-to-leaf paths are:

["1->2->5", "1->3"]

题目分析:其实这类型题采用的递归方法,递归最主要的是考虑一个模式,也就是需要考虑返回的模型,需要考虑什么时候返回,然后按照某个顺序来进行递归实现。

代码实现:

class Solution(object):
    def binaryTreePaths(self, root):
        """
        :type root: TreeNode
        :rtype: List[str]
        """
        list = []
        if root:
            if root.left is None and root.right is None:
                return [str(root.val)]
            if root.left:
                for str1 in self.binaryTreePaths(root.left):
                    list.append(str(root.val) + "->" + str1)
            if root.right:
                for str1 in self.binaryTreePaths(root.right):
                    list.append(str(root.val) + "->" + str1)
        return list

感觉自己代码经过总结,提炼之后,有了很明显的进步,但仍然需要不断的努力。
看一下大佬们的代码:

def binaryTreePaths(self, root):
    if not root:
        return []
    return [str(root.val) + '->' + path
            for kid in (root.left, root.right) if kid
            for path in self.binaryTreePaths(kid)] or [str(root.val)]

第一感觉就是666啊,真的是一波神操作啊。大佬们把代码中的左右节点的遍历写在了一起,利用了复合语句,这种语句对我这种菜鸟来说,掌握还是有一点点困难,不过,不怕,我可以记住啊。

def binaryTreePaths(self, root):
    if not root:
        return []
    if not root.left and not root.right:
        return [str(root.val)]
    treepaths = [str(root.val)+'->'+path for path in self.binaryTreePaths(root.left)]
    treepaths += [str(root.val)+'->'+path for path in self.binaryTreePaths(root.right)]
    return treepaths

在这里中,采用的是not root来判断 root == None,也采用了复合语句,在这里,很具有参考意义哦~~哈哈~,可以学习一波。

大佬们用的深度优先遍历来求解问题。

# dfs + stack
def binaryTreePaths1(self, root):
    if not root:
        return []
    res, stack = [], [(root, "")]
    while stack:
        node, ls = stack.pop()
        if not node.left and not node.right:
            res.append(ls+str(node.val))
        if node.right:
            stack.append((node.right, ls+str(node.val)+"->"))
        if node.left:
            stack.append((node.left, ls+str(node.val)+"->"))
    return res

# bfs + queue
def binaryTreePaths2(self, root):
    if not root:
        return []
    res, queue = [], collections.deque([(root, "")])
    while queue:
        node, ls = queue.popleft()
        if not node.left and not node.right:
            res.append(ls+str(node.val))
        if node.left:
            queue.append((node.left, ls+str(node.val)+"->"))
        if node.right:
            queue.append((node.right, ls+str(node.val)+"->"))
    return res

# dfs recursively
def binaryTreePaths(self, root):
    if not root:
        return []
    res = []
    self.dfs(root, "", res)
    return res

def dfs(self, root, ls, res):
    if not root.left and not root.right:
        res.append(ls+str(root.val))
    if root.left:
        self.dfs(root.left, ls+str(root.val)+"->", res)
    if root.right:
        self.dfs(root.right, ls+str(root.val)+"->", res)

需要,好好看一波深度优先遍历算法,明确他们的思想是怎样的。

猜你喜欢

转载自blog.csdn.net/angela2016/article/details/79587427