其他分享
首页 > 其他分享> > 【leetcode】深度优先和广度优先102、433

【leetcode】深度优先和广度优先102、433

作者:互联网

102. 二叉树的层序遍历

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

433. 最小基因变化

    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