Skip to content

Commit 845962c

Browse files
committed
update 257
1 parent f51f720 commit 845962c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tree/Yu/257.py

+30
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,33 @@ def binaryTreePaths(self, root):
3131
if node.right:
3232
stack.append((node.right, strr + str(node.val) + "->"))
3333
return res
34+
35+
36+
# Recursive DFS
37+
# Definition for a binary tree node.
38+
# class TreeNode(object):
39+
# def __init__(self, x):
40+
# self.val = x
41+
# self.left = None
42+
# self.right = None
43+
44+
class Solution(object):
45+
def binaryTreePaths(self, root):
46+
"""
47+
:type root: TreeNode
48+
:rtype: List[str]
49+
"""
50+
self.res = []
51+
#Edge
52+
if not root:
53+
return []
54+
self.dfs(root, "")
55+
return self.res
56+
57+
def dfs(self, root, strr):
58+
if not root.left and not root.right:
59+
self.res.append(strr + str(root.val))
60+
if root.left:
61+
self.dfs(root.left, strr + str(root.val) + "->")
62+
if root.right:
63+
self.dfs(root.right, strr + str(root.val) + "->")

0 commit comments

Comments
 (0)