From 9b770de8cccfee4588a4966086aa4f285c347d68 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 17:07:10 -0500 Subject: [PATCH] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 4cafbded..195b6f94 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -497,7 +497,7 @@ class Solution: ``` -递归法+回溯(版本二) +递归法+隐形回溯(版本一) ```Python # Definition for a binary tree node. # class TreeNode: @@ -505,7 +505,6 @@ class Solution: # self.val = val # self.left = left # self.right = right -import copy from typing import List, Optional class Solution: @@ -523,13 +522,13 @@ class Solution: if not node.left and not node.right: result.append('->'.join(map(str, path))) else: - self.generate_paths(node.left, copy.copy(path), result) - self.generate_paths(node.right, copy.copy(path), result) - path.pop() + # path[:] 是隐藏回溯 + self.generate_paths(node.left, path[:], result) + self.generate_paths(node.right, path[:], result) ``` -递归法+隐形回溯 +递归法+隐形回溯(版本二) ```Python # Definition for a binary tree node. # class TreeNode: