这题跟I的区别仅仅是结果是倒序的。所以偷懒最后返回的结果用的倒序。。。
# 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 levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root == None:
return []
a = [root]
b = []
c = []
d = []
while a != []:
for i in range(len(a)):
#可以用双向队列的leftpop来写
last = a[0]
del a[0]
c.append(last.val)
if last.left or last.right:
if last.left:
b.append(last.left)
if last.right:
b.append(last.right)
else:
pass
d.append(c)
a = b[::]
b = []
c = []
return d[::-1]