【leetcode】深度优先和广度优先102、433
作者:互联网
def levelOrder(self, root):
ans, level = [], [root]
while root and level:
ans.append([node.val for node in level])
level = [kid for n in level for kid in (n.left, n.right) if kid]
return ans
q = collections.deque()
q.append([s,s,0])
while q:
prev,cur,steps = q.popleft()
if cur==e: return steps
for b in bank:
if b!=prev and sum([s1!=s2 for s1,s2 in zip(b,cur)])==1:
q.append([cur,b,steps+1])
return -1
标签:优先,return,cur,level,ans,102,leetcode,append,kid 来源: https://blog.csdn.net/KaelCui/article/details/112637181