-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path107.py
49 lines (46 loc) · 1.19 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
from collections import defaultdict
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root is None:
return []
frontier = []
frontier.append(root)
level = [] #defaultdict(list)
depth = 0
ans = []
val = []
val.append(root.val)
while frontier:
level.append(frontier)
ans.append(val)
next = []
val = []
for u in frontier:
if u.left is not None:
next.append(u.left)
val.append(u.left.val)
if u.right is not None:
next.append(u.right)
val.append(u.right.val)
depth +=1
frontier=next
ans.reverse()
return ans
if __name__=='__main__':
t = TreeNode(3)
t.left=TreeNode(9)
t.right=TreeNode(20)
t.right.left=TreeNode(15)
t.right.right=TreeNode(7)
s = Solution()
k = s.levelOrderBottom(t)
print(k)