-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Paths
More file actions
25 lines (24 loc) · 799 Bytes
/
Binary Tree Paths
File metadata and controls
25 lines (24 loc) · 799 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
# 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: Optional[TreeNode]) -> List[str]:
answer=[]
def backtrace(root,path=[]):
if not root:
return
if not root.left and not root.right:
path.append(str(root.val))
answer.append("".join(path.copy()))
path.pop()
return
add = str(root.val)+"->"
path.append(add)
backtrace(root.left,path)
backtrace(root.right,path)
path.pop()
backtrace(root)
return answer