# 二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
示例:
二叉树:[3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层序遍历结果:
[ [3], [9,20], [15,7] ]## template ```python class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): def levelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ if not root: return [] queue, res = [root], [] while queue: size = len(queue) temp = [] for i in range(size): data = queue.pop(0) temp.append(data.val) if data.left: queue.append(data.left) if data.right: queue.append(data.right) res.append(temp) return res ``` ## 答案 ```python ``` ## 选项 ### A ```python ``` ### B ```python ``` ### C ```python ```