LeetCode笔记

二叉树的所有路径

2018-03-20  本文已影响4人  只为此心无垠

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
上一篇 下一篇

猜你喜欢

热点阅读