树的层序遍历的总结 一些层序遍历的相关问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

def (self, root: TreeNode) -> int:
if root is None:
return 0
xx = 1
queue = [root]
while queue:
cache = []

for q in queue:
if q.left is None and q.right is None:
# 此处是离根最近的一个叶子结点
pass
if q.left is not None:
cache.append(q.left)
if q.right is not None:
cache.append(q.right)
xx += 1 # 此时xx是cache中结点所在的层数
queue = cache

一些层序遍历的相关问题

  1. 层序遍历
  2. “倒”层序遍历
  3. 求最大层数
  4. 求离根节点最近的叶子结点所在层数
  5. 待补充。。。