-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0257_binary_tree_paths.py
More file actions
43 lines (34 loc) · 1.15 KB
/
0257_binary_tree_paths.py
File metadata and controls
43 lines (34 loc) · 1.15 KB
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
from utils.binary_tree_helper import arr_to_binary_tree_helper
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def binaryTreePaths(self, root: 'TreeNode'):
def path_helper(cur, res, temp, level):
if cur:
while len(temp) > level:
temp.pop()
temp += [cur.val]
path_helper(cur.left, res, temp, level + 1)
path_helper(cur.right, res, temp, level + 1)
if not cur.left and not cur.right:
if len(temp) > 1:
s = str(temp[0])
for i in temp[1:]:
s += "->" + str(i)
res.append(s)
else:
res.append(str(temp[0]))
if not root:
return []
res = []
path_helper(root, res, [], 0)
return res
if __name__ == '__main__':
# begin
s = Solution()
arr = [1, 2, 3, None, 5]
print(s.binaryTreePaths(arr_to_binary_tree_helper(arr)))