Printing from the top binary tree (python)

Title Description

Downward from the print out of each node of the binary tree, with the layer node from left to right printing.
. 1  class Solution:
 2      # returns the value of each node list from top to bottom, for example: [l, 2,3] 
. 3      DEF PrintFromTopToBottom (Self, the root):
 . 4          # Write code here Wallpaper 
. 5          IF the root == None:
 . 6              return [ ]
 . 7          Queue = []
 . 8          queue.append (the root)
 . 9          I = 0
 10          the while I < len (Queue):
 . 11              the root = Queue [I]
 12 is              Queue [I] = root.val
 13 is              IF root.left:
 14                 queue.append(root.left)
15             if root.right:
16                 queue.append(root.right)
17             i+=1
18         return queue

 

Guess you like

Origin www.cnblogs.com/NPC-assange/p/12020666.html