257. Binary Tree Paths

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 + "->")
        
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容