0112.路径总和 python 精简递归返回条件

0112.路径总和 python 精简113递归返回条件
This commit is contained in:
SianXiaoCHN
2022-03-25 20:25:06 -05:00
parent dd8bea8335
commit f8392906ac

View File

@ -496,11 +496,10 @@ class solution:
def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]: def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]:
def traversal(cur_node, remain): def traversal(cur_node, remain):
if not cur_node.left and not cur_node.right and remain == 0: if not cur_node.left and not cur_node.right:
result.append(path[:]) if remain == 0:
return result.append(path[:])
return
if not cur_node.left and not cur_node.right: return
if cur_node.left: if cur_node.left:
path.append(cur_node.left.val) path.append(cur_node.left.val)