从上往下打印二叉树 牛客网 剑指Offer

从上往下打印二叉树 牛客网 剑指Offer

  • 题目描述
  • 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    #run:31ms memory:5712k
    def PrintFromTopToBottom(self, root):
        lt = []
        if root is None:
            return lt
        queue = []
        queue.append(root)
        while len(queue) > 0:
            currentRoot = queue.pop(0)
            lt.append(currentRoot.val)
            if currentRoot.left:
                queue.append(currentRoot.left)
            if currentRoot.right:
                queue.append(currentRoot.right)
        return lt

 

你可能感兴趣的:(Algorithm,牛客网,算法,剑指Offer)