/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
  public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int>> res;
        if (pRoot == NULL) return res;
        queue<TreeNode*> nodeList; // 记录每层节点
        nodeList.push(pRoot);
        bool isReverse = false;        // 判断当前层数组是否翻转第一层默认不翻转
        while (nodeList.size()) {
            vector<int> row;
            int size = nodeList.size();
            while (size--) { // 遍历当前层节点
                // 当前层值加入数组
                row.push_back(nodeList.front()->val);
                // 将下一层节点放入
                if (nodeList.front()->left) nodeList.push(nodeList.front()->left);
                if (nodeList.front()->right) nodeList.push(nodeList.front()->right);
                // 当前节点丢弃
                nodeList.pop();
            }
            if (isReverse) { // 翻转数组
                reverse(row.begin(), row.end());
            }
            // 当前层值加入返回值
            res.push_back(row);
            isReverse = !isReverse;    // 取反
        }
        return res;
    }
};
static const auto io_sync_off = []() { //lambda函数
    // turn off sync,关闭输入输出流的缓存
    std::ios::sync_with_stdio(false);
    // untie in/out streams,实现输入和输出流的解绑
    std::cin.tie(nullptr);
    return nullptr;
}
();