题解 | #求二叉树的层序遍历#python 11行代码

求二叉树的层序遍历

http://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3

class Solution:
    def levelOrder(self , root ):
        # write code here
        if not root:return []
        queue = [root] 
        res = []
        while queue:
            res.append([node.val for node in queue])
            temp = []
            for node in queue:
                if node.left:temp.append(node.left)
                if node.right:temp.append(node.right)
            queue = temp
        return res
全部评论
好棒
点赞 回复 分享
发布于 2021-12-19 11:39

相关推荐

每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-19 20:55
因为业务不是喜欢的,所以就没去,现在实习工作也有很多dirtywork,很后悔,怎么能舔回这个offer啊
flmz_Kk:试一试跟hr舔回来,不过保不齐米的活也有很多dirtywork,只能说不要美化自己没走过的路
点赞 评论 收藏
分享
评论
6
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务