File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -31,3 +31,33 @@ def binaryTreePaths(self, root):
31
31
if node .right :
32
32
stack .append ((node .right , strr + str (node .val ) + "->" ))
33
33
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 ) + "->" )
You can’t perform that action at this time.
0 commit comments