# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None from collections import deque classSolution(object): defzigzagLevelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ flag = 1 ifnot root:return [] q = deque() q.append(root) res = [] while q: n = len(q) tmp = [] for i in range(n): node = q.popleft() tmp.append(node.val) if node.left:q.append(node.left) if node.right:q.append(node.right) if flag==1: res.append(tmp) flag = 0 else: res.append(tmp[::-1]) flag = 1 return res