Update 0257.二叉树的所有路径.md
This commit is contained in:
parent
9b770de8cc
commit
1a672740e9
|
|
@ -512,19 +512,19 @@ class Solution:
|
||||||
if not root:
|
if not root:
|
||||||
return []
|
return []
|
||||||
result = []
|
result = []
|
||||||
self.generate_paths(root, [], result)
|
self.traversal(root, [], result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def generate_paths(self, node: TreeNode, path: List[int], result: List[str]) -> None:
|
def traversal(self, cur: TreeNode, path: List[int], result: List[str]) -> None:
|
||||||
if not node:
|
if not cur:
|
||||||
return
|
return
|
||||||
path.append(node.val)
|
path.append(cur.val)
|
||||||
if not node.left and not node.right:
|
if not cur.left and not cur.right:
|
||||||
result.append('->'.join(map(str, path)))
|
result.append('->'.join(map(str, path)))
|
||||||
else:
|
if cur.left:
|
||||||
# path[:] 是隐藏回溯
|
self.traversal(cur.left, path[:], result)
|
||||||
self.generate_paths(node.left, path[:], result)
|
if cur.right:
|
||||||
self.generate_paths(node.right, path[:], result)
|
self.traversal(cur.right, path[:], result)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue