-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path257_binary_tree_paths.py
More file actions
50 lines (43 loc) · 1.42 KB
/
257_binary_tree_paths.py
File metadata and controls
50 lines (43 loc) · 1.42 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
44
45
46
47
48
49
50
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from binarytree import buildtree, inorder_tree_print
# NOT MY SOLUTION, https://walkccc.me/LeetCode/problems/257/#__tabbed_1_3
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: Optional[TreeNode]
:rtype: List[str]
"""
if root is None:
return root
def tree_paths(root, path, pathes):
if root is None:
return
# reached leaf node add path to the pathes
if root.left is None and root.right is None:
paths.append(''.join(path) + str(root.val))
return
path.append(str(root.val) + '->')
tree_paths(root.left, path, paths)
tree_paths(root.right, path, paths)
path.pop()
paths = []
tree_paths(root, [], paths)
return paths
vectors = [
[1], ['1'],
[1,2,3,None,5], ['1->2->5','1->3']
]
for i in range(0, len(vectors), 2):
a = vectors[i]
expected = vectors[i+1]
tree = buildtree(a)
print(a)
inorder_tree_print(tree)
print('')
returned = Solution().binaryTreePaths(tree)
assert expected == returned, f'for {a} expected {expected}, but returned {returned}!'