剑指 从上到下打印二叉树

从上往下打印二叉树

http://www.nowcoder.com/questionTerminal/7fe2212963db4790b57431d9ed259701

层序遍历 树

import collections
class Solution:
    # 返回从上到下每个节点值列表,例:[1,2,3]
    def PrintFromTopToBottom(self, root):
        # write code here
        queue=collections.deque([root])
        visit=[]
        result=[]
        if root==None:
            return result
        while queue:
            root=queue.popleft()
            if root.left:
                queue.append(root.left)
            if root.right:
                queue.append(root.right)
            if root not in visit:
                result.append(root.val)
                visit.append(root)
        return result
全部评论

相关推荐

mjasjon:这种trash中厂 简历过筛概率比大厂还低(除阿里系)
投递哔哩哔哩等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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