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

求二叉树的层序遍历

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
全部评论

相关推荐

点赞 评论 收藏
分享
09-09 21:23
门头沟学院 Java
程序员牛肉:小牛肉来也! 主要就是没有实习经历。因为你的投递方向肯定是中小厂。在小厂中,很少会有公司愿意花钱培养你。因此会更加青睐有实习的同学。再加上你的学历比较差一点,所以找不到是正常的。 跟简历项目啥的已经没有大关系了,就是差一份实习。秋招和日常实习一起投递吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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