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