# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
al=[]
if not root:
return al
stack=[root]
ls=[]
tem=[]
while(stack):
index=stack.pop(0)
if index:
ls.append(index.val)
tem.append(index.left)
tem.append(index.right)
if not stack and ls:
stack=tem[:]
al.append(ls[:])
ls=[]
tem=[]
return al
本文介绍了一种二叉树的层次遍历算法,通过使用列表和临时列表来跟踪每一层的节点值,最终返回一个包含所有层级节点值的列表。
193

被折叠的 条评论
为什么被折叠?



