class TreeNode:
def __init__(self,val):
self.val = val
self.lchild = None
self.rchild = None
def FloorOrder(root):
su = []
su.append(root)
for item in su:
if item.lchild:
su.append(item.lchild)
if item.rchild:
su.append(item.rchild)
for i in su:
print(i.val,end=" ")
a = TreeNode(3)
b = TreeNode(1)
c = TreeNode(4)
d = TreeNode(6)
f = TreeNode(2)
g = TreeNode(7)
a.lchild = b
a.rchild = f
b.lchild = c
b.rchild = d
c.lchild = g
root = a
FloorOrder(root)
简单实现层序遍历
本文介绍了一种简单的层序遍历二叉树的方法。通过使用列表存储节点,并逐层遍历的方式实现了对二叉树的遍历。示例代码创建了一个包含多个节点的二叉树,并使用层序遍历方法打印了每个节点的值。
3171

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



