# 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 []
que = [root]
res = [[root.val]]
while que:
tmp = []
for _ in range(len(que)):
node = que.pop(0)
if node.left:
que.append(node.left)
tmp.append(node.left.val)
if node.right:
que.append(node.right)
tmp.append(node.right.val)
if tmp:
res.append(tmp)
return res
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)