The
topic description prints out each node of the two-fork tree from the top down, and the same layer node prints from left to right. Train of thought: Use two list, one to install node, one to load node value. For this list of loaded nodes, we pop up the front node each time and add the left and right nodes to the back in turn. In any case, this set of node list can guarantee that the node from top to bottom, the same layer of nodes from left to right to add, pop-up. The code is as follows:
Class TreeNode ():
def __init__ (self,x):
self.val = x
self.left = none
Self.right = None
def function (Root):
a = [] result
= []
if not root: return result
a.append (root) while
A:
Current_root = A.pop (0)
result.append (current_root.val)
if current_root.left:
a.append (Current_ Root.left)
if current_root.right:
a.append (Current_root.right)