题目描述:
第一次提交:参考113-路径总和②
class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: r = [] if not root: return r l = "" def path(root, l): if not root: return l += str(root.val) if not root.left and not root.right: r.append(l) l += "->" path(root.left,l) path(root.right,l) path(root,l) return r
方法二
class Solution: def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if root == None: return [] if root.left == None and root.right == None: return [str(root.val)] paths = self.binaryTreePaths(root.left) + self.binaryTreePaths(root.right) for i in range(len(paths)): paths[i] = str(root.val) + ‘->‘ + paths[i] return paths
原文:https://www.cnblogs.com/oldby/p/10616747.html