题解 | #从上往下打印二叉树#

从上往下打印二叉树

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

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回从上到下每个节点值列表,例:[1,2,3]
    def PrintFromTopToBottom(self, root):
        # write code here
        que = []
        ret = []
        if root is None:
            return
        que.append(root)
        
        while que:
            que_tmp = []
            for item in que:
                ret.append(item.val)
#                 print(item.val)
                if item.left:
                    que_tmp.append(item.left)
                if item.right:
                    que_tmp.append(item.right)
            que = que_tmp
#             print(que)
        return ret
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 11:31
点赞 评论 收藏
分享
晗江雪:其实我只是觉得你们导员说的很好笑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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