-
Notifications
You must be signed in to change notification settings - Fork 171
/
107.py
31 lines (30 loc) · 843 Bytes
/
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
# 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):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
from collections import deque
if root is None:
return []
levels=list()
q=deque([root])
levels.append([i.val for i in q])
target=root
while q:
node=q.popleft()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if node==target:
if q:
levels.append([i.val for i in q])
target=q[-1]
return list(reversed(levels))