题解 | #求二叉树的层序遍历#Python 无引用queue包

求二叉树的层序遍历

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

class Solution:
    def levelOrder(self , root ):
        # write code here
        p = root
        if not p:
            return None
        list = []
        list1 = []
        list1.append(p)
        while list1:
            list2 = []   #存放指针
            list3 = []   #存放val值
            for i in range(len(list1)):
                list3.append(list1[i].val)
                if list1[i].left:
                    list2.append(list1[i].left)
                if list1[i].right:
                    list2.append(list1[i].right)
            list.append(list3)
            list1 = list2
        return list
全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-12-08 17:10
拼多多 算法 38x18 大专
李橙子:你的白菜价我做梦都遥不可及
点赞 评论 收藏
分享
2025-12-27 22:35
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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