mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 21:50:49 +08:00
Update 0257.二叉树的所有路径.md
This commit is contained in:
@ -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:
|
||||
|
Reference in New Issue
Block a user