-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0257.py
More file actions
36 lines (30 loc) · 941 Bytes
/
q0257.py
File metadata and controls
36 lines (30 loc) · 941 Bytes
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
#!/usr/bin/python3
from typing import List
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: TreeNode) -> List[str]:
result = []
path = []
def dfs_vist(node: TreeNode):
if node:
path.append(node.val)
if not node.left and not node.right:
result.append(path.copy())
else:
if node.left:
dfs_vist(node.left)
if node.right:
dfs_vist(node.right)
path.pop()
dfs_vist(root)
result_str = []
for nums in result:
ans = ""
for num in nums:
ans += "->" + str(num)
result_str.append(ans[2:])
return result_str