标签:end one for none res init return poi 层次遍历
# 利用队列进行层次遍历就行
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def Print(self, pRoot):
if not pRoot:
return []
queue = [pRoot]
outList = []
while queue:
res = []
nextQueue = []
for point in queue: # 这里再遍历每一层
res.append(point.val)
if point.left:
nextQueue.append(point.left)
if point.right:
nextQueue.append(point.right)
outList.append(res)
queue = nextQueue #覆盖的作用
return outList[::-1]
t = TreeNode(0)
t.left = TreeNode(1)
t.right = TreeNode(2)
t.left.left = TreeNode(3)
t.left.right = TreeNode(6)
t.right.left = TreeNode(7)
s=Solution()
print(s.Print(t))
标签:end one for none res init return poi 层次遍历
原文地址:https://www.cnblogs.com/limingqi/p/12467847.html