# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
# 与LC102一样,唯一的区别就是最后返回result,反转一下
# 1.BFS
result = []
if not root:
return result
queue = collections.deque()
queue.append(root)
while queue:
level = []
size = len(queue)
for i in range(size):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(level)
return result[::-1]
70-BFS--LC107二叉树的层序遍历II
最新推荐文章于 2024-11-10 14:06:08 发布