这道题就非常有意思了,看起来和之前遇到过一个“之”字输出二叉树很相似,但是注意输出格式要是[[8],[6,10],[5,7,9,11]]而不是[[8],[6],[10],[5],[7],[9],[11]]

class Solution:
    # 返回二维列表[[1,2],[4,5]]
    def Print(self, pRoot):
        # write code here
        if not pRoot:
            return []
        stack = [pRoot]
        res1 = []
        res = []
        while stack:
            sz = len(stack)#此处用sz变量单独写一行主要是为了方便debug的时候看变量值
            res1 = []
            for i in range(sz):#必须必须要写这一行,不然的话,{8,6,10,5,7,9,11}的输出就会是[[8],[6],[10],[5],[7],[9],[11]]而不是[[8],[6,10],[5,7,9,11]]
                node = stack.pop(0)
                res1.append(node.val)
                if node.left:
                    stack.append(node.left)
                if node.right:
                    stack.append(node.right)
            res.append(res1)
        return res