Binary Tree Paths*
1
/ \
2 3
\
5["1->2->5", "1->3"]class Solution(object):
def binaryTreePaths(self, root):
if root == None:
return []
result = []
self.dfs(root, result, '')
return result
def dfs(self, root, result, strs):
if root == None:
return
s = s + '->' + str(root.val) if s != '' else str(root.val)
if root.left == None and root.right == None:
result.append(strs)
if root.left:
self.dfs(root.left, result, strs)
if root.right:
self.dfs(root.right, result, strs)Last updated