def allBinaryTreePaths(self, root):
if root == None:
return
if len(self.result) == 0:
self.result += (str(root.val))
else:
# if root.val != None:#排除空节点
self.result += ('->'+ str(root.val))
if root.left == None and root.right == None:
self.resultAll.append(self.result)
self.allBinaryTreePaths(root.left)
self.allBinaryTreePaths(root.right)
length = len(self.result)
temp = self.result[0:length-2]
self.result = temp
def binaryTreePaths(self, root):
# write your code here
self.resultAll = []
self.result = ''
self.allBinaryTreePaths(root)
return self.resultAll
网友评论