此题可以直接用BFS求解,先用BFS将每一层的结点存起来,然后用个if判断下是从左往右打印还是从印往左打印就可以了,把BFS得到node list转化成value list即可。

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param pRoot TreeNode类 
# @return int整型二维数组
#
class Solution:
    def Print(self , pRoot: TreeNode) :
        # write code here
        if pRoot == None:
            return []
        node_layer = [[pRoot]]
        level = 0
        while True:
            tmp_i = [] # 下一层的node list
            for tmp_node in node_layer[level]:
                if tmp_node.left:
                    tmp_i.append(tmp_node.left)
                if tmp_node.right:
                    tmp_i.append(tmp_node.right)
            if len(tmp_i) == 0:
                break
            level += 1
            node_layer.append(tmp_i)
        val_list = []
        for i in range(len(node_layer)):
            tmp_i = []
            for j in range(len(node_layer[i])):
                if i % 2==0:
                    tmp_i.append(node_layer[i][j].val)
                else:
                    tmp_i.insert(0,node_layer[i][j].val)
            val_list.append(tmp_i)
        return val_list
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.right.left = TreeNode(4)
root.right.right = TreeNode(5)
print(Solution().Print(root))