We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fc11a58 commit d51850bCopy full SHA for d51850b
binary_tree_paths.py
@@ -0,0 +1,21 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ # @param {TreeNode} root the root of the binary tree
5
+ # @return {List[str]} all root-to-leaf paths
6
+ def binaryTreePaths(self, root):
7
+ # Write your code here
8
+ self.ret = []
9
+ self._binaryTreePaths(root, '')
10
+ return self.ret
11
12
+ def _binaryTreePaths(self, root, path):
13
+ if root:
14
+ path += str(root.val) if not path else ('->' + str(root.val))
15
+ # print path
16
+ if root.left:
17
+ self._binaryTreePaths(root.left, path)
18
+ if root.right:
19
+ self._binaryTreePaths(root.right, path)
20
+ if not (root.left or root.right):
21
+ self.ret.append(path)
0 commit comments