题解 | #求二叉树的层序遍历#

求二叉树的层序遍历

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

用deque数据结构

from collections import deque
class Solution:
    def levelOrder(self , root ):
        # write code here
        if not root:
            return None
        q = deque([])
        q.append(root)
        res = []
        while q:
            size = len(q)
            temp = []
            for i in range(size):
                curr = q.popleft()
                temp.append(curr.val)
                if curr.left:
                    q.append(curr.left)
                if curr.right:
                    q.append(curr.right)

            res.append(temp)
        return res
全部评论

相关推荐

08-21 10:11
已编辑
南京邮电大学 Java
Java后端劝退第一...:我mentor也人很好,感觉就是同龄人,昨天出去散步看他摘了根狗尾巴草一直转,特别搞笑
你被mentor骂过吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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