欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Python剑指offer:分行从上到下打印二叉树

发布时间:2025/4/16 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python剑指offer:分行从上到下打印二叉树 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

从上到下按层打印二叉树,同一层的节点按照从左到右
的顺序打印,每一层打印到第一行,例如本题中上一个问题的二叉树
输出形式会是:
8
6 10
5 7 9 11

这道题和前面一道题十分类似,也可以用一个队列来保存要打印的节点。
为了把二叉树的每一行单独打印到一行里,我们需要两个变量:
一个变量表示当前层中还没有打印的节点数;另一个变量表示下一层节点数。

class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = None class Solution:def Print(self, root):if not root:return Nonequeue = [root]toBePrinted = 1 # 表示当前层中还没有打印的节点数nextLevel = 0 # 表示下一层的节点数while len(queue) > 0:currentRoot = queue.pop(0)# 按空格隔开,不换行输出print(currentRoot.val, end=" ")if currentRoot.left:queue.append(currentRoot.left)nextLevel += 1if currentRoot.right:queue.append(currentRoot.right)nextLevel += 1toBePrinted -= 1# 如果当前层未打印的节点数为0,就跳转到下一层if toBePrinted == 0:# 如果下一层没有东西了,就不再执行程序了if nextLevel == 0:breakprint("\n")toBePrinted = nextLevelnextLevel = 0pNode1 = TreeNode(8) pNode2 = TreeNode(6) pNode3 = TreeNode(10) pNode4 = TreeNode(5) pNode5 = TreeNode(7) pNode6 = TreeNode(9) pNode7 = TreeNode(11)pNode1.left = pNode2 pNode1.right = pNode3 pNode2.left = pNode4 pNode2.right = pNode5 pNode3.left = pNode6 pNode3.right = pNode7S = Solution() S.Print(pNode1)

总结

以上是生活随笔为你收集整理的Python剑指offer:分行从上到下打印二叉树的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。