题解 | #按之字形顺序打印二叉树#
按之字形顺序打印二叉树
http://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def Print(self, pRoot): # write code here # write code here result = [] if not pRoot: return result level_nodes = [pRoot] rev = True while(level_nodes): new_level_nodes = [] result.append([]) for i in level_nodes: result[-1].append(i.val) if i.left: new_level_nodes.append(i.left) if i.right: new_level_nodes.append(i.right) level_nodes = new_level_nodes if not rev: result[-1] = result[-1][-1::-1] rev = not rev return result