100天每日一题(day13)

解答:转眼就入秋了,天凉的真快,感觉夏天还没过多久就要到冬天了,

和树相关的题基本就是DFS,主要是思考DFS的输入是什么,在哪里返回

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

 

class Solution:

    out=[]

    def binaryTreePaths(self, root: TreeNode) -> List[str]:

        if not root:

            return []

        out=[]

 

        def DFS(root,path):

            path+=str(root.val)

            if root.left==None and root.right==None:

                out.append(path)

            elif not root.right:

                DFS(root.left,path+'->')

            elif not root.left:

                DFS(root.right,path+'->')

            else:

                DFS(root.left,path+'->')

                DFS(root.right,path+'->')

        DFS(root,'')

        return out

 

相关文章:

  • 2021-05-18
  • 2022-02-17
  • 2021-08-11
  • 2021-11-19
  • 2021-12-21
  • 2021-08-23
  • 2022-03-08
  • 2021-06-11
猜你喜欢
  • 2021-09-05
  • 2021-05-16
  • 2021-11-28
  • 2021-10-21
  • 2021-07-19
  • 2022-12-23
相关资源
相似解决方案