mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 05:20:59 +08:00
Update 0257.二叉树的所有路径.md
This commit is contained in:
@ -470,7 +470,7 @@ class Solution {
|
|||||||
## Python:
|
## Python:
|
||||||
|
|
||||||
|
|
||||||
递归法+回溯(版本一)
|
递归法+回溯
|
||||||
```Python
|
```Python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -561,16 +561,11 @@ class Solution:
|
|||||||
迭代法:
|
迭代法:
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
from collections import deque
|
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
"""二叉树的所有路径 迭代法"""
|
|
||||||
|
|
||||||
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
||||||
# 题目中节点数至少为1
|
# 题目中节点数至少为1
|
||||||
stack, path_st, result = deque([root]), deque(), []
|
stack, path_st, result = [root], [str(root.val)], []
|
||||||
path_st.append(str(root.val))
|
|
||||||
|
|
||||||
while stack:
|
while stack:
|
||||||
cur = stack.pop()
|
cur = stack.pop()
|
||||||
|
Reference in New Issue
Block a user