BFS模板,记住这5个: (1)针对树的BFS 1.1 无需分层遍历 from collections import deque def levelOrderTree(root): if not root: return q = deque([root]) while q: head = q.popleft() do something with this head node... if head.left: q.append(head.left) if head.right: q.append…