/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
#include <algorithm>
#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pRoot TreeNode类 
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > Print(TreeNode* pRoot) {
        // write code here
        // 广度优先搜索稍改

        if(!pRoot)
            return {};

        deque<TreeNode*> d;
        d.emplace_back(pRoot);

        // 判断改行要不要反转数组
        bool flag=false;
        vector<vector<int>> ans;
        while(!d.empty())
        {
            int len=d.size();
            vector<int> temp;
            for(int i=0; i<len; ++i)
            {
                temp.emplace_back(d.front()->val);
                if(d.front()->left)
                    d.emplace_back(d.front()->left);
                if(d.front()->right)
                    d.emplace_back(d.front()->right);
                d.pop_front();
            }

            if(flag)
                reverse(temp.begin(), temp.end());
            flag = !flag;

            ans.emplace_back(temp);
        }

        return ans;
    }
};