美文网首页Leetcode
257. Binary Tree Paths

257. Binary Tree Paths

作者: oo上海 | 来源:发表于2016-08-03 07:31 被阅读7次

    257. Binary Tree Paths

    题目:

    https://leetcode.com/problems/binary-tree-paths/

    难度:

    Easy

    Tag : tree, DFS

    类似的题目是Path Sum Ⅱ

    第一眼看起来不像easy题目,然后堵了一下自己写的Path Sum Ⅱ自己的解答

    同一种花样

    class Solution:
        # @param {TreeNode} root
        # @return {string[]}
        def binaryTreePaths(self, root):
            if root == None: return []
            result = []
            self.auxTreePaths(root,result,"")
            return result
            
        
        def auxTreePaths(self,root,result,curStr):
            if root == None:
                return
            curStr += str(root.val)
            if root.left == None and root.right == None:
                result.append(curStr)
            if root.left:
                self.auxTreePaths(root.left,result,curStr + "->")
            if root.right:
                self.auxTreePaths(root.right,result,curStr + "->")
            
    

    相关文章

      网友评论

        本文标题:257. Binary Tree Paths

        本文链接:https://www.haomeiwen.com/subject/dwdqsttx.html