-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path257.py
37 lines (33 loc) · 879 Bytes
/
257.py
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
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def __init__(self):
self.l = []
def dfs(self, root, v):
if root is None:
return
v.append(root.val)
if root.left is None and root.right is None:
self.l.append("->".join(str(i) for i in v))
return
else:
if root.left is not None:
self.dfs(root.left,v)
if root.right is not None:
self.dfs(root.right,v)
v.pop()
return
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if root is None:
return
v = []
self.dfs(root,v)
return self.l