按之字形列印二叉樹
阿新 • • 發佈:2018-12-22
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回二維列表[[1,2],[4,5]] def Print(self, pRoot): # write code here if not pRoot: return [] tree = [pRoot] result = [] count = 1 while tree: subtree = [] row = [] for i in tree: row.append(i.val) if i.left: subtree.append(i.left) if i.right: subtree.append(i.right) if count%2==0: row.reverse() result.append(row) tree = subtree count += 1 return result