# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
result = []
que = deque()
que.append(root)
while que:
n = len(que)
result.append([])
for _ in range(n):
current = que.popleft()
result[-1].append(current.val)
if current.left:
que.append(current.left)
if current.right:
que.append(current.right)
return result