-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path257.py
73 lines (50 loc) · 1.7 KB
/
257.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# leetcode 257 二叉树的所有路径
# 第一种 回溯递归法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
if not root:
return []
path = ""
result = []
self.getPath(root, result, path)
return result
def getPath(self, root, result, path):
# 这个函数是是用来进行回溯递归的
path += str(root.val)
if not root.left and not root.right:
result.append(path)
if root.left:
self.getPath(root.left, result, path + "->")
if root.right:
self.getPath(root.right, result, path + "->")
return
## 接下来用的是迭代法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
result = []
path = [str(root.val)]
stack = [root]
while stack:
node = stack.pop()
tmpPath = path.pop()
if not node.left and not node.right:
result.append(tmpPath)
if node.right:
stack.append(node.right)
path.append(tmpPath + "->" + str(node.right.val))
if node.left:
stack.append(node.left)
path.append(tmpPath + "->" + str(node.left.val))
return result