- Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathbinary-tree-paths.py
More file actions
Latest commit
23 lines (20 loc) · 764 Bytes
/
Copy pathbinary-tree-paths.py
File metadata and controls
23 lines (20 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# coding: utf-8
classSolution:
# @param {TreeNode} root the root of the binary tree
# @return {List[str]} all root-to-leaf paths
defbinaryTreePaths(self, root):
# Write your code here
self.ret= []
self._binaryTreePaths(root, '')
returnself.ret
def_binaryTreePaths(self, root, path):
ifroot:
path+=str(root.val) ifnotpathelse ('->'+str(root.val))
# print path
ifroot.left:
self._binaryTreePaths(root.left, path)
ifroot.right:
self._binaryTreePaths(root.right, path)
ifnot (root.leftorroot.right):
self.ret.append(path)
# easy: http://lintcode.com/zh-cn/problem/binary-tree-paths/