-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_257_binary_tree_paths.py
More file actions
44 lines (33 loc) · 1.1 KB
/
Copy pathtest_257_binary_tree_paths.py
File metadata and controls
44 lines (33 loc) · 1.1 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
import unittest
"""
write down thoughts
"""
from typing import List
# 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: TreeNode) -> List[str]:
res = []
def btPath(prefix, node):
if not node.left and not node.right:
res.append(prefix + str(node.val))
return # finish recursion at leaf node
if node.left:
btPath(prefix + str(node.val) + '->', node.left)
if node.right:
btPath(prefix + str(node.val) + '->', node.right)
btPath("", root)
return res
class TestSolution(unittest.TestCase):
def test1(self):
node5 = TreeNode(val=5)
node2 = TreeNode(val=2, right=node5)
node3 = TreeNode(val=3)
node1 = TreeNode(val=1, left=node2, right=node3)
self.assertEqual(["1->2->5", "1->3"], Solution().binaryTreePaths(node1))
if __name__ == '__main__':
unittest.main()