-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0257-binary-tree-paths.py
More file actions
36 lines (29 loc) · 961 Bytes
/
0257-binary-tree-paths.py
File metadata and controls
36 lines (29 loc) · 961 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
# time complexity: O(nlogn)
# space complexity: O(n)
from typing import List, Optional
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 = []
def backtrack(node: Optional[TreeNode], path: str):
if node:
path += str(node.val)
if not node.left and not node.right:
result.append(path)
else:
path += "->"
backtrack(node.left, path)
backtrack(node.right, path)
backtrack(root, "")
return result
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(3)
root1.left.right = TreeNode(5)
print(Solution().binaryTreePaths(root1))
root2 = TreeNode(1)
print(Solution().binaryTreePaths(root2))