# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution: # @param root, a tree node # @return a list of lists of integers deflevelOrder(self, root): ifnot root: return [] result=[] queue=[] queue.append(root) count=len(queue) s=[] while len(queue)!=0: tmp=queue[0] s.append(tmp.val) del queue[0] count-=1 if tmp.left: queue.append(tmp.left) if tmp.right: queue.append(tmp.right) if count==0: result.append(s) s=[] count=len(queue) return result