forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_paths.py
More file actions
23 lines (20 loc) · 764 Bytes
/
binary_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
class Solution:
# @param {TreeNode} root the root of the binary tree
# @return {List[str]} all root-to-leaf paths
def binaryTreePaths(self, root):
# Write your code here
self.ret = []
self._binaryTreePaths(root, '')
return self.ret
def _binaryTreePaths(self, root, path):
if root:
path += str(root.val) if not path else ('->' + str(root.val))
# print path
if root.left:
self._binaryTreePaths(root.left, path)
if root.right:
self._binaryTreePaths(root.right, path)
if not (root.left or root.right):
self.ret.append(path)
# easy: http://lintcode.com/zh-cn/problem/binary-tree-paths/