mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 13:45:39 +08:00
Update 0257.二叉树的所有路径.md
This commit is contained in:
@ -497,7 +497,7 @@ class Solution:
|
|||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
递归法+回溯(版本二)
|
递归法+隐形回溯(版本一)
|
||||||
```Python
|
```Python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -505,7 +505,6 @@ class Solution:
|
|||||||
# self.val = val
|
# self.val = val
|
||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
import copy
|
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -523,13 +522,13 @@ class Solution:
|
|||||||
if not node.left and not node.right:
|
if not node.left and not node.right:
|
||||||
result.append('->'.join(map(str, path)))
|
result.append('->'.join(map(str, path)))
|
||||||
else:
|
else:
|
||||||
self.generate_paths(node.left, copy.copy(path), result)
|
# path[:] 是隐藏回溯
|
||||||
self.generate_paths(node.right, copy.copy(path), result)
|
self.generate_paths(node.left, path[:], result)
|
||||||
path.pop()
|
self.generate_paths(node.right, path[:], result)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
递归法+隐形回溯
|
递归法+隐形回溯(版本二)
|
||||||
```Python
|
```Python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
|
Reference in New Issue
Block a user