-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_4.py
More file actions
69 lines (61 loc) · 1.41 KB
/
question_4.py
File metadata and controls
69 lines (61 loc) · 1.41 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# @Time : 2018/5/13 下午12:52
# @Author : cancan
# @File : question_4.py
# @Function : 二叉树的层次遍历
"""
Question:
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
Example:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
"""
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
r = [[root.val]]
n = []
t = []
if root.left:
t.append(root.left)
n.append(root.left.val)
if root.right:
t.append(root.right)
n.append(root.right.val)
if n:
r.append(n)
while t:
p = []
n = []
for i in t:
if i.left:
p.append(i.left)
n.append(i.left.val)
if i.right:
p.append(i.right)
n.append(i.right.val)
if n:
r.append(n)
t = p
return r