1 Star 1 Fork 0

bywuuu/pyTest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
从上到下打印二叉树.py 853 Bytes
一键复制 编辑 原始数据 按行查看 历史
bywuuu 提交于 2019-12-10 20:46 . add
# 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Soluation:
def print_tree(self,root):
l = []
if not root:
return []
r = [root]
while len(r):
temp = r.pop(0)
l.append(temp.val)
if temp.left:
r.append(temp.left)
if temp.right:
r.append(temp.right)
return l
node1 = TreeNode(1)
node2 = TreeNode(2)
node3 = TreeNode(3)
node4 = TreeNode(4)
node5 = TreeNode(5)
node6 = TreeNode(6)
node7 = TreeNode(7)
node1.left = node2
node1.right = node3
node2.left = node4
node2.right = node5
node3.left = node6
node3.right = node7
s = Soluation()
print(s.print_tree([]))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bywuuu/pyTest.git
[email protected]:bywuuu/pyTest.git
bywuuu
pyTest
pyTest
master

搜索帮助