Skip to content

Commit d51850b

Browse files
committed
二叉树的所有路径
1 parent fc11a58 commit d51850b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

binary_tree_paths.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)