forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_01_PrintTreeFromTopToBottom.py
More file actions
55 lines (43 loc) · 1.05 KB
/
32_01_PrintTreeFromTopToBottom.py
File metadata and controls
55 lines (43 loc) · 1.05 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# @File : 32_01_PrintTreeFromTopToBottom.py
# @Date : 2020-10-31
# @Author : tc
"""
剑指 Offer 32 - I. 从上到下打印二叉树
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回:
[3,9,20,15,7]
提示:
节点总数 <= 1000
实际就是BFS遍历
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
import collections
from typing import List
class Solution:
def levelOrder(self, root: TreeNode) -> List[int]:
if not root:
return []
res, queue = [], collections.deque()
queue.append(root)
while queue:
node = queue.popleft()
res.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return res
if __name__ == '__main__':
pass