-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
107.py
70 lines (58 loc) · 2.05 KB
/
107.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
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
70
__________________________________________________________________________________________________
sample 28 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
def node_values(nodes):
values = []
for node in nodes:
if node is not None:
values.append(node.val)
return values
def node_childs(nodes):
childs = []
for node in nodes:
if node is not None:
childs.append(node.left)
childs.append(node.right)
return childs
answer = []
queue = [root]
while queue:
values = node_values(queue)
if values:
answer.insert(0,values)
queue = node_childs(queue)
return answer
__________________________________________________________________________________________________
sample 13128 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
res, s, q = [], [], [root]
while q:
temp = []
for i in range(len(q)):
v = q.pop()
if v.left != None:
q.insert(0,v.left)
if v.right != None:
q.insert(0,v.right)
temp.append(v.val)
s.append(temp)
while s:
res.append(s.pop())
return res
__________________________________________________________________________________________________