Update 0257.二叉树的所有路径.md

This commit is contained in:
jianghongcheng
2023-05-22 17:07:10 -05:00
committed by GitHub
parent 2024fc272a
commit 9b770de8cc

View File

@ -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: