# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def construct_paths(self,root, path='',paths =[]):
if root:
path += str(root.val)
if not root.left and not root.right:
paths.append(path)
else:
path += '->'
self.construct_paths(root.left, path)
self.construct_paths(root.right, path)
return paths
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
else:
return self.construct_paths(root)
if not root.left and not root.right:
return [str(root.val)]
提交代码时,我收到以下错误答案: 输入: [1] 输出: [“ 1-> 2-> 5”,“ 1-> 3”,“ 1”] 预期: [“ 1”] 但是当我在测试用例中输入错误的输入时: 您的输入 [1] 输出量 [“ 1”] 预期 [“ 1”]