#bfs
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root TreeNode类
# @return int整型二维数组
#
class Solution:
def levelOrder(self , root: TreeNode) -> List[List[int]]:
# write code here
if root is None: # 判断根节点是否为空
return
res = []
q = [root]
while q:
tep = []
for _ in range(len(q)): # 对于每一层进行遍历
node = q.pop(0)
tep.append(node.val)
# 判断左右节点是否存在
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
res.append(tep)
return res