-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path257.二叉树的所有路径.py
37 lines (31 loc) · 925 Bytes
/
257.二叉树的所有路径.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
# @lc app=LeetCode.cn id=257 lang=python3
#
# [257] 二叉树的所有路径
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
self.res = []
def preorder(root, track):
if root == None:
return
if root.left == None and root.right == None:
track.append(root.val)
self.res.append('->'.join(str(node) for node in track))
track.pop()
return
# 遍历
track.append(root.val)
preorder(root.left, track)
preorder(root.right, track)
track.pop()
preorder(root, [])
return self.res
# @lc code=end